Set 
`required=False`<https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.Field.required>on
 your `password` field`.

If you want to make sure it's only non-mandatory when the user exists 
override your `UserForm.__init__` this way:

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    def __init__(self, *args **kwargs):
        super(UserForm, self).__init__(*args, **kwargs)
        self.fields['password'].required = self.instance.pk is None

    ....

Le jeudi 5 septembre 2013 08:22:31 UTC-4, Domagoj Kovač a écrit :
>
> Hi guys,
>
> i am working on user CRUD. 
> I am using one form for user creation and update. When user is initially 
> created it is ok to perform password validation - users must have password, 
> but in my edit form i want to have a logic like: when nothing is entered 
> don't do anything, when user enters something in password field override 
> old password. The problem is that password field is always mandatory. Is 
> there some way to disable validation according to my logic.
>
> This is my form
> from django import forms
> from django.contrib.auth.models import User
>
> class UserForm(forms.ModelForm):
>     password = forms.CharField(widget=forms.PasswordInput)
>
>     class Meta:
>         model = User
>         exclude = ('last_login', 'date_joined')
>
>     def save(self, commit=True):
>         user = super(UserForm, self).save(commit=False)
>         password = self.cleaned_data["password"]
>         if user.pk:
>             if password:
>                 user.set_password(self.cleaned_data["password"])
>         else:
>             user.set_password(self.cleaned_data["password"])
>         if commit:
>             user.save()
>         return user
>
> and my view
> @login_required
> @user_passes_test(lambda u: u.groups.filter(name='Users').count() == 1, 
> login_url='/access-denied/')
> def form(request, pk=None):
>     if request.POST:
>         if pk:
>             target = User.objects.get(pk=pk)
>             form = UserForm(request.POST, instance=target)
>         else:
>             form = UserForm(request.POST)
>
>         if form.is_valid():
>             form.save()
>             return HttpResponseRedirect('/user/list')
>     else:
>         if pk:
>             target = User.objects.get(pk=pk)
>             form = UserForm(instance=target)
>         else:
>             form = UserForm()
>
>     args = {}
>     args.update(csrf(request))
>
>     args['form'] = form
>
>     return render_to_response('users/form.html', args, 
> context_instance=RequestContext(request))
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to