What is the correct way to validate the data in a model instance?

Which is preferable to use: checking values for a field in the database at the application level, for example: `def save(self, force_insert=False, force_update=False, use=None, update_fields=None):

    `if update_fields is not None:
        self.full_clean(exclude=update_fields)
    else:
        self.full_clean()
      
    super().save(force_insert=force_insert, force_update=force_update, 
                     using=using, update_fields=update_fields,)`.

Or is the validation at the database level?

It depends upon what needs to be validated.

If you’ve got fields that are restricted to values based upon some business logic, and you don’t have a suitable set of constraints in the database, then you need to perform the validation in Django.

Otherwise, it’s an architectural decision based on other considerations. There’s no “best” or “right” answer for every circumstance. There are always tradeoffs.

1 Like