[Answered ]-Django: Is it possible to add constraint on model level only (Not on DB level)

1👍

AFAIK – No…

But you can do it programmatically with obeying some rules. First of all, you shouldn’t declare constraints as usual in model’s fields definitions and Meta.

Second you should override save method of models, which you want containt (https://docs.djangoproject.com/en/3.1/topics/db/models/#overriding-model-methods).

Third, it leads from the link’s note – Overridden model methods are not called on bulk operations – so you should avoid modifing methods invoked from .objects, e.g. .object.update(). I’m not sure about .objects.create – would be great to test it or find docs link.

👤404pio

Leave a comment