I am using standard Django Models and ModelForms. 

On the Model, I am overriding the clean() method to check that 2 fields in 
combination are valid. I am having validators[], not nulls etc defined on 
other fields.

In the ModelForm, I am not modifying anything. I only set the corresponding 
Model in the ModelForm's Meta class.

When I try to create a new object through the form, the *individual* fields 
in the ModelForm/Model *are not validated* when I call form.is_valid() in 
the View. According to the docs (
https://docs.djangoproject.com/en/1.6/ref/models/instances/#validating-objects) 
the is_valid() method on the Form should call the Model's clean_fields()method 
(first). 

This doesn't seem to work when you submit a form without a Model instance 
(or a new instance not in the db). When I'm editing an existing object, all 
is well. It nicely triggers invalid values in individual fields before 
calling the Model's clean() method.

*When I remove the overridden clean() method from my Model, all is 
well.*Individual fields are validated both when creating new objects and 
editing 
existing ones.

I have also tested this with the admin module's forms. It has exactly the 
same behaviour.

So the question is, why does overriding the clean() method on my Model 
prevent the ModelForm validating the individual fields before calling the 
clean() method to test additional cross-field stuff???

Note that I am not validating the ModelForm. All validation is on the Model 
itself.

Model:

class Survey(models.Model):
    from_date = models.DateField(null=False, blank=False, validators=[...])
    to_date = models.DateField(null=False, blank=False, validators=[...])
    (...)    
    def clean(self):
        errors = []
        # At this point I expect self.to_date already to be validated for not 
null etc.
        # It isn't for new Model instances, only when editing an existing one
        if self.to_date < self.from_date:
            errors.append(ValidationError("..."))

ModelForm:

class TestForm(ModelForm):
    class Meta:
        model = Survey

View (to render blank form for new Model data entry):

(...)if request.method == "POST":
    survey_form = TestForm(request.POST)
    if '_save' in request.POST:
        if survey_form.is_valid():
            survey_form.save()
            return HttpResponseRedirect(next_url)
    else:
        return HttpResponseRedirect(next_url)else:
    survey_form = TestForm()

context = {'form': survey_form}(...)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9242e49b-ce4d-4e51-b063-8331c06acbe9%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to