Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why, in Model.full_clean, is clean_fields called before clean?

Is there a reason, in Model.full_clean, for clean_fields be called before clean? In my project, it would be more reasonable if I could first do my validations to then validate clean_fields stuff, like in this situation:

class MyModel(models.Model):
    status = models.IntegerField(blank=False, null=False)

    def clean(self):
        if not self.status:
            self.status = 0

Would be any problem changing this ordering in full_clean?

like image 735
zVictor Avatar asked Nov 23 '25 13:11

zVictor


1 Answers

Why would it be "more reasonable"? As it's name describes, clean_fields is designed to clean each field individually, whereas the purpose of clean is only to validate field interactions. For example, one field can be blank only if another field is set, etc. The clean_fields method is called first to ensure that you can actually validate these interactions; if the fields have errors in general, it's pointless to validate them in conjunction.

like image 195
Chris Pratt Avatar answered Nov 25 '25 11:11

Chris Pratt