Anthony
Anthony

Reputation: 979

What does clean mean in Django?

This is an obvious question for everyone, but I don't understand what the term "clean" means. I can use clean_data, and use form validation on my forms. However, I still don't understand what this means.

In order to use validation, do I always need to use the keyword "clean"?

Upvotes: 0

Views: 4485

Answers (2)

SHUBHAM GUPTA
SHUBHAM GUPTA

Reputation: 31

As far as i have understood ..all the implicit validations are performed using clean.. which is performed when we check for the form validity using is_valid:

but we can add our own validations to it by overriding the function clean(): what we have to do is we call the super().clean() such that all the implicit validations are done by the clean() defined by django and then add our own validations to it if necessary...

when you call the super().clean() it returns the dictionary containing the cleaned_data.. you can store it in a variable ... or else you can access the dictionary using self.cleaned_data

Upvotes: 2

iklinac
iklinac

Reputation: 15738

As stated in documentation

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. This method returns the clean data, which is then inserted into the cleaned_data dictionary of the form.

Clean is preventing dirty data in DB

Upvotes: 4

Related Questions