{{{
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.
* status: new => closed
* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => Bug
* resolution: => invalid
Comment:
Please don't use Trac as a support channel. If you accept `None` values
you need to handle them in comparisons, see also
[https://docs.djangoproject.com/en/2.2/ref/models/instances/#django.db.models.Model.clean
documentation].
Closing per TicketClosingReasons/UseSupportChannels.
--
Ticket URL: <https://code.djangoproject.com/ticket/31041#comment:1>