Lack of validation at the object level.

71 views
Skip to first unread message

Михаил Итпрогер

unread,
Jun 30, 2021, 6:49:04 AM6/30/21
to django-d...@googlegroups.com

Hello, I am currently developing a reusable application using the Django framework.

It just so happens that the instances of the models of this application will most often be created inside the code, and not in the forms.
I've created validators for fields that only work in forms, but I would like to reuse them for creating/updating objects as well.
The Django documentation says that validation only occurs on forms, but it is not written for what reason.

I have tried many solutions to deal with this problem, but they all have a significant drawback - when creating an instance from a form, the validation happens twice.

For example, with __setattr__():  once when the concstruct_instance() is called, the second time when full_clean().

With save(): first full_clean(), and then my validation in save().
I'm really interested in why the opportunity to validate everything at the object level was missed, because when creating objects outside the forms, you can enter any values ​​into the fields that contradict the restrictions.

I could solve this problem, which would require small changes in the validation logic, but first I need to consult with you.

Besides, maybe you can tell me an alternative solution.

Adam Johnson

unread,
Jun 30, 2021, 9:45:44 AM6/30/21
to django-d...@googlegroups.com
What validation do you need? Much can be done in the database with constraints, especially check constraints.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/E619D0E2-3B55-478F-895B-0F59848BAFA6%40hxcore.ol.

Args Kwargs

unread,
Jun 30, 2021, 11:01:19 AM6/30/21
to django-d...@googlegroups.com
Here's what I mean:

ср, 30 июн. 2021 г. в 16:45, 'Adam Johnson' via Django developers (Contributions to Django itself) <django-d...@googlegroups.com>:
Снимок экрана (41).png

Adam Johnson

unread,
Jun 30, 2021, 11:43:58 AM6/30/21
to django-d...@googlegroups.com
"Unique restriction will be ignored" - this is not the case. Uniqueness is enforced by the database, so as long as your migrations have been built and run correctly, the database will error when trying to insert a duplicate unique value. If you're finding that you can create duplicates on unique fields, check your database setup.

For the case of checking whether a string contains a certain character, or matches a format, it's possible to enforce in the database with a check constraint. I've written several blog posts on the topic: https://duckduckgo.com/?sites=adamj.eu&q=check+constraints&ia=web . None exactly align with such string validation, but if you understand the concepts you should be able to figure it out. Admittedly it's more complicated than writing a validator function.

Args Kwargs

unread,
Jun 30, 2021, 11:59:08 AM6/30/21
to django-d...@googlegroups.com
Thank you so much.

ср, 30 июн. 2021 г. в 18:43, 'Adam Johnson' via Django developers (Contributions to Django itself) <django-d...@googlegroups.com>:
Reply all
Reply to author
Forward
0 new messages