[Django] #31041: Custom validation in the clean method triggers before checking the fields exist or not

4 views
Skip to first unread message

Django

unread,
Nov 27, 2019, 2:26:09 PM11/27/19
to django-...@googlegroups.com
#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.

Django

unread,
Nov 28, 2019, 2:13:46 AM11/28/19
to django-...@googlegroups.com
#31041: Custom validation in the clean method triggers before checking the fields
exist or not.
-------------------------------------+-------------------------------------
Reporter: tahmidkhan | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* 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>

Reply all
Reply to author
Forward
0 new messages