django-admin forms and model constraints

hi all -
I’ve got a unique_together type constraint (applied as UniqueConstraint() in class Meta but the behaviour of the admin is a bit disappointing, because if a user carelessly submits a duplication, it isn’t handled as a form error.
The form is valid because the form validation doesn’t include the uniqueness constraint. As this is the admin, I am hoping there is a simple declaration I can make. Otherwise I guess its custom form and custom form validation for each model with a constraint.

1 Like

A Constraint is something added to the database - it’s not code executed by the model, those checks are performed by the database engine. So no, those constraints are not validated before save.

You can add a clean method on the Model to perform “in Django” validation of fields rather than adding custom forms in the admin.