#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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/010701906e6860c5-f2965928-e92f-4dff-b542-42cbe89e72f0-000000%40eu-central-1.amazonses.com.