HackSoftware / Django-Styleguide

Django styleguide used in HackSoft projects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Constraints instead of clean(), full_clean()

mkurnikov opened this issue · comments

What do you think about moving all the logic from custom clean() methods on the model to the db constraints?
https://docs.djangoproject.com/en/2.2/ref/models/constraints/

Yes, some logic is not representable as a constraint, it could be an exception which could still reside inside the clean() method. But otherwise, constraints are always enforced, there's no need to remember to call full_clean() method. Also, there are usually faster than python validation.

@mkurnikov I actually like this idea. If we can push more validation towards the database, this is less code to write.

I'll add this as an additional idea for doing validations. If you have any good examples to provide, please do so 👍

@mkurnikov Added this - 3c40f63

If you feel there's a need for more explaining & examples, feel free to open a PR.

Until then, I'll close this issue 🙌