[Fixed]-Basic validation disabled when overriding clean method in Django modelforms

1👍

The basic validation you’re talking about is invoked via the BaseForm‘s clean method, when you overrode this method, you hid the underlying validation.

You need to call the ModelForms base clean method via super in your clean method

   def clean(self):
       super(FormProfile, self).clean()

As shown in the example in the docs, you will still need to check if a value is none in your check so it would become

 if number1 and number2 and number1 >= number2:

You could provide a default value with .get but I wouldn’t recommend it as it would make this check a little harder to verify correct results

👤Sayse

0👍

def clean(self):
    # your validation
    return super(FormProfile, self).clean()
👤Pavan

Leave a comment