Aliquis Aliquis - 6 months ago 26
Python Question

Form validation in Django: What to override or not?

I do not yet fully understand the validation process of Forms in Django. I've read the documentation about it already, but I've still questions.

In the Mozilla Django Tutorial you can read this:

Django provides numerous places where you can validate your data. The
easiest way to validate a single field is to override the method
clean_() for the field you want to check. So for example,
we can validate that entered renewal_date values are between now and 4
weeks by implementing clean_renewal_date() as shown below.

My question now:
If I create methods like clean_field_name() what do I override? In the quote above it says that you'll override this method. However, I could not find what exactly I do override.

In case I do override something, isn't it bad? Or are those methods only methods you can create by your own but do not exist by default?

Answer Source

The tutorial is (partly) wrong - the clean_YOURFIELDNAME methods are not overrides as there's no default clean_YOURFIELDNAME method to be overridden. It's just that the form validation will look for those methods and if they are defined call them as part as the validation process.

For the implementation details, the simplest is to read the source:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download