Re: [Django] #34166: UniqueConstraint with condition not validated if the condition field is not editable.

8 views
Skip to first unread message

Django

unread,
Nov 17, 2022, 3:54:44 AM11/17/22
to django-...@googlegroups.com
#34166: UniqueConstraint with condition not validated if the condition field is not
editable.
-------------------------------------+-------------------------------------
Reporter: Márton Salomváry | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: model validation | Triage Stage:
unique constraint condition | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson):

For your project, defining model form subclass for
[https://docs.djangoproject.com/en/4.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_form
ModelAdmin.get_form] implementing clean validating the non-editable field
would be the way to go.

--
Ticket URL: <https://code.djangoproject.com/ticket/34166#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 17, 2022, 7:03:54 AM11/17/22
to django-...@googlegroups.com
#34166: UniqueConstraint with condition not validated if the condition field is not
editable.
-------------------------------------+-------------------------------------
Reporter: Márton Salomváry | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: model validation | Triage Stage:
unique constraint condition | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Márton Salomváry):

Replying to [comment:2 Mariusz Felisiak]:
> Thanks for the ticket. This is
[https://docs.djangoproject.com/en/stable/topics/forms/modelforms
/#interaction-with-model-validation the documented behavior for forms] and
it was [https://github.com/django/django/pull/14625#discussion_r946747147
discussed] shortly in the original PR (as you've already noticed).
>
> Unfortunately, changing this would be backward incompatible so please
first start a discussion on the DevelopersMailingList, where you'll reach
a wider audience and see what other think, and
[https://docs.djangoproject.com/en/stable/internals/contributing/bugs-and-
features/#requesting-features follow the guidelines with regards to
requesting features].

I understand, but I'm not sure I have the courage and time to try to sell
a breaking change :)


Replying to [comment:3 Carlton Gibson]:


> For your project, defining model form subclass for
[https://docs.djangoproject.com/en/4.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_form
ModelAdmin.get_form] implementing clean validating the non-editable field
would be the way to go.


I ended up adding this to the model as a simple workaround (and a
handcrafted `violation_error_message` on the `UniqueConstraint`):
{{{
def clean(self):
conditional_constraints = {"thing_unique_name"}
for constraint in self._meta.constraints:
if constraint.name in conditional_constraints:
constraint.validate(self.__class__, self)

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34166#comment:4>

Reply all
Reply to author
Forward
0 new messages