#31041: Custom validation in the clean method triggers before checking the 
fields
exist or not
-----------------------------------------+------------------------
               Reporter:  tahmidkhan     |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Uncategorized  |        Version:  2.2
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 I have the following code in my models file in Django:


 {{{
 class MyModel(models.Model):
         ...
     foo = models.IntegerField()
     bar = models.IntegerField()

     def validate_foo_bar(self):
         self._validation_errors = {}
         if self.foo > self.bar:
             self._validation_errors['foo'] = ['Must be greater than bar.']
             self._validation_errors['bar'] = ['Must be less than foo.']

     def clean(self):
         self.validate_foo_bar()
         if bool(self._validation_errors):
                 raise ValidationError(self._validation_errors)
         super(MyModel, self).clean()
 }}}



 Hopefully the idea is clear. I check for errors in the clean method and
 raise them if they occur. When I use an admin form to create an object, if
 I leave the `foo` and `bar` fields empty, I get the following error:


 {{{
 if self.foo > self.bar:
 TypeError: '>' not supported between instances of 'NoneType' and
 'NoneType'
 }}}


 Why is this happening? Shouldn't the requirement check trigger before the
 method I wrote? Thanks for any help.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31041>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.a1501c18149cb421c133fcc2d9f335cc%40djangoproject.com.

Reply via email to