I realize this question has sort of been asked here and here and elsewhere.
...but the responses seem a little inconclusive. I thought perhaps the following concrete example might invite some specific reasons why this is or is not a good strategy.
So, what's wrong with just doing this? Excluded fields and defaults seem to work just fine. Certain ORM methods won't call save(), but then you wouldn't leverage ModelForms either. Is there something else I'm missing? Is it just backwards compatibility?
from django.db import models
from django.core.exceptions import ValidationError
class Person(models.Model):
favorite_food = models.CharField(max_length=250)
def clean(self, *args, **kwargs):
if self.favorite_food == 'bacon':
raise ValidationError('Bacon is not good for you!')
super(Person, self).clean(*args, **kwargs)
def save(self, *args, **kwargs):
self.full_clean()
super(Person, self).save(*args, **kwargs)
Doesn't it seem like a good rule of thumb to at least put validation in the model first, in the interest of DRY, "fat models"? ...And to only move validation logic out to ModelForms where the validation context might depend on fields outside of the model, say, for instance, the request.user's permissions? (And even then, if it's that important, maybe they should be in the model anyway?)
I really like validating against bacon there, in one place, instead of here, in the ModelFrom and again in and Admin form... Hoping to hear some solid arguments against doing just that!