On Tue, 19 Oct 2021 10:44:01 +0300
Tal Einat <
tale...@gmail.com> wrote:
> Hi folks,
>
> For those who've worked with Django a lot: What are your experiences,
> or thoughts, with making Django models call full_clean() on save(), to
> validate things that can't be validated at the DB level? (By
> overriding the save() method or registering a pre_save signal
> handler.)
>
Model validation was added in the 1.2 or 1.3 version, and if I recall
correctly, the assumption already back then was that it's a good idea
to add it when you write new code -- I don't remember exactly the
reasons for not doing it by default; I'm guessing somewhere between
backwards-compatibility and the fact that, since most of the same
validations are performed by model-forms anyway, and some of them can be
non-trivial (e.g. including database access for uniqueness checks), it
might cause undue inefficiency (back then, a very large majority of the
interaction with models was done through forms).
> If also using DRF, are there cases where you'd prefer validations to
> be on the model itself rather than its DRF serializer?
>
I'd say it depends on how much the models get saved outside of
serializers.
HTH,
Shai.