Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modelform: override clean method

I have two questions concerning the clean method on a modelform. Here is my example:

class AddProfileForm(ModelForm):
        ...
        password = forms.CharField(max_length=30,widget=forms.PasswordInput(attrs={'class':'form2'}))
        password_verify = forms.CharField(max_length=30,widget=forms.PasswordInput(attrs={'class':'form2'}), label='Retype password')
        ...

        class Meta:
            model = UserModel
            fields=("username", "password", "password_verify", "first_name", "last_name", "date_of_birth", "biography", "contacts", )

        #called on validation of the form
        def clean(self):
            #run the standard clean method first
            cleaned_data=super(AddProfileForm, self).clean()
            password = cleaned_data.get("password")
            password_verify = cleaned_data.get("password_verify")

            #check if passwords are entered and match
            if password and password_verify and password==password_verify:
                print "pwd ok"
            else:
                raise forms.ValidationError("Passwords do not match!")

            #always return the cleaned data
            return cleaned_data
  1. Should I always call the standard clean method?

    cleaned_data=super(AddProfileForm, self).clean()
    
  2. Should I always return the cleaned_data variable?

    return cleaned_data
    
like image 998
rom Avatar asked Aug 22 '13 04:08

rom


People also ask

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

What is clean method in Django?

The clean() method on a Field subclass is responsible for running to_python() , validate() , and run_validators() in the correct order and propagating their errors. If, at any time, any of the methods raise ValidationError , the validation stops and that error is raised.

How do you remove this field is required Django?

If yes try to disable this behavior, set the novalidate attribute on the form tag As <form action="{% url 'new_page' %}", method="POST" novalidate> in your html file.

What is ModelForm in Django?

Django Model Form It is a class which is used to create an HTML form by using the Model. It is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time.


1 Answers

For 1, Yes, if you want to make use of parent class's validators. See this explanation on the doc.

Warning

The ModelForm.clean() method sets a flag that makes the model validation step validate the uniqueness of model fields that are marked as unique, unique_together or unique_for_date|month|year.

If you would like to override the clean() method and maintain this validation, you must call the parent class’s clean() method.

For 2, yes, if data validates properly. Otherwise raise validation error.

like image 157
Rohan Avatar answered Nov 18 '22 20:11

Rohan