﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31041	Custom validation in the clean method triggers before checking the fields exist or not.	Tahmid	nobody	"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."	Bug	closed	Database layer (models, ORM)	2.2	Normal	invalid			Unreviewed	0	0	0	0	0	0
