[Django] #23354: ModelChoiceField does not handle MultipleObjectsReturned

11 views
Skip to first unread message

Django

unread,
Aug 23, 2014, 11:48:34 AM8/23/14
to django-...@googlegroups.com
#23354: ModelChoiceField does not handle MultipleObjectsReturned
----------------------------+--------------------
Reporter: kezabelle | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
As far as I can gather [that is, this hasn't actually bitten me], the
[https://github.com/django/django/blob/deed00c0d803d324a3dfdeba52458b6b009c1a90/django/forms/models.py#L1182
to_python] for `ModelChoiceField` does not catch the queryset model's
`MultipleObjectsReturned` exception, which in most cases will be fine
because the query will be against the `pk`, which is ostensibly unique
unless a user has gone out of their way to break things.

However, given the ability to use `to_field_name` (which was recently
documented [in #21860], increasing the likelihood of it's usage) it is
feasible that a user would end up using a field which is not unique (or
even spans a relationship?), and thus in a simple `key:value` lookup could
return multiple rows.

This would also technically affect the `ModelMultipleChoiceField`, whose
superclass is `ModelChoiceField` and whose `to_python` method is used for
each value.

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

Django

unread,
Aug 24, 2014, 12:42:02 PM8/24/14
to django-...@googlegroups.com
#23354: ModelChoiceField does not handle MultipleObjectsReturned
---------------------------+--------------------------------------

Reporter: kezabelle | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------+--------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Shouldn't we just document that `to_field_name` must be able to uniquely
identify the objects? I'm not sure what error message we'd present since
it's presumably a programming error and not something a user could fix.

--
Ticket URL: <https://code.djangoproject.com/ticket/23354#comment:1>

Django

unread,
Aug 27, 2014, 7:45:32 AM8/27/14
to django-...@googlegroups.com
#23354: ModelChoiceField does not handle MultipleObjectsReturned
---------------------------+--------------------------------------
Reporter: kezabelle | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------+--------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"83762da3fbc00fdb5c6cfbcd04e25ff00c599102"]:
{{{
#!CommitTicketReference repository=""
revision="83762da3fbc00fdb5c6cfbcd04e25ff00c599102"
Fixed #23354 -- Documented that ModelChoiceField to_field_name should be a
unique field.

Thanks Keryn Knight for the suggestion.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23354#comment:2>

Django

unread,
Aug 27, 2014, 7:50:59 AM8/27/14
to django-...@googlegroups.com
#23354: ModelChoiceField does not handle MultipleObjectsReturned
---------------------------+--------------------------------------
Reporter: kezabelle | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------+--------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"e86030b3be2ac9af4a2855d88802c0b750152c8a"]:
{{{
#!CommitTicketReference repository=""
revision="e86030b3be2ac9af4a2855d88802c0b750152c8a"
[1.7.x] Fixed #23354 -- Documented that ModelChoiceField to_field_name


should be a unique field.

Thanks Keryn Knight for the suggestion.

Backport of 83762da3fb from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23354#comment:3>

Reply all
Reply to author
Forward
0 new messages