[Django]-Django: difference between is_valid and form_valid

4👍

is_valid on the surface just tells you whether or not the form is valid, and thats the only job it should ever do..

From the source code:

def is_valid(self):

    """
    Returns True if the form has no errors. Otherwise, False. If errors are
    being ignored, returns False.
    """
    return self.is_bound and not self.errors

Underneath this, what it also does is (from docs)

run validation and return a boolean designating whether the data was valid:

The validation is ran because errors is a property that will call full_clean if the validation hasn’t been called yet.

 @property
 def errors(self):

    "Returns an ErrorDict for the data provided for the form"
    if self._errors is None:
        self.full_clean()
    return self._errors

Where should I create the code that update those fields, and why?

In the form_valid method because by this point you’ve found out that your validation has verified that it is safe to update your model.

👤Sayse

Leave a comment