[Django] #35569: Misleading ValidationError from `limit_choices_to` violation

8 views
Skip to first unread message

Django

unread,
Jul 1, 2024, 9:07:38 AM7/1/24
to django-...@googlegroups.com
#35569: Misleading ValidationError from `limit_choices_to` violation
-------------------------------------+-------------------------------------
Reporter: Jacob | Owner: Jacob Walls
Walls |
Type: | Status: assigned
Cleanup/optimization |
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The ValidationError raised when a choice violating
`ForeignKey.limit_choices_to` is made refers to an instance "not
existing", when in reality, the instance may exist but is simply not an
allowed choice.

With the
[https://docs.djangoproject.com/en/4.2/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to
example field]:
{{{
In [5]: Person(staff_member_id=2).full_clean()
---------------------------------------------------------------------------
ValidationError Traceback (most recent call
last)
Cell In[5], line 1
----> 1 Person(staff_member_id=2).full_clean()

File ~/release/lib/python3.12/site-packages/django/db/models/base.py:1502,
in Model.full_clean(self, exclude, validate_unique, validate_constraints)
1499 errors = e.update_error_dict(errors)
1501 if errors:
-> 1502 raise ValidationError(errors)

ValidationError: {'staff_member': ['user instance with id 2 does not
exist.']}

In [6]: User.objects.get(id=2)
Out[6]: <User: anonymous>
}}}

We could use wording like "not a valid choice" and reuse that wording for
nonexistent instances as well if there is a disclosure concern about
introducing a distinction between the two cases.
--
Ticket URL: <https://code.djangoproject.com/ticket/35569>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Reply all
Reply to author
Forward
0 new messages