简体   繁体   中英

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_<fieldname>() 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?

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: https://github.com/django/django/blob/master/django/forms/forms.py#L379

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM