[Django] #35640: ModelMultipleChoiceField fail when the foreignkey remote model is a child class of anohter model

33 views
Skip to first unread message

Django

unread,
Jul 30, 2024, 2:22:34 AM7/30/24
to django-...@googlegroups.com
#35640: ModelMultipleChoiceField fail when the foreignkey remote model is a child
class of anohter model
-------------------------------+--------------------------------------
Reporter: Omar BENHAMID | Type: Bug
Status: new | Component: Forms
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
== The issue

When a child model is used as a foreign key, forms with
ModelMultipleChoiceField (not setting to_field_name) will systematically
fail to validate submited values.


== Reproduction

models.py
{{{
class A(models.Model):
pass

class B(A):
pass

class C(models.Model):
fkfield = models.ForeignKey(B, on_delete=models.CASCADE)
}}}

Create form with MultipleChoiceField of the foreignkey fkfield and it will
systematically fail to validate.

== Reason

ModelMultipleChoiceField uses value.pk in prepare_value to generate
choices but uses "to_field_name" in _check_data for validation.
default to_field_name is by default related_field.field_name which is
"<parent>_ptr" in the case of inheriance child models, which points to the
parent itself (not the pk).

ModelMultipleChoiceField fail when the foreignkey remote model is a child
class of anohter model
== Workaround

Use to_field_name = 'pk' when ModelMultipleChoiceField is created.

== Proposed fix

Use PK in validation like in generation of choices.

Spotted in 3.2, still exist in latest HEAD, probably existed before ...
--
Ticket URL: <https://code.djangoproject.com/ticket/35640>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 30, 2024, 2:27:36 AM7/30/24
to django-...@googlegroups.com
#35640: ModelMultipleChoiceField fail when the foreignkey remote model is a child
class of anohter model
-------------------------------+--------------------------------------
Reporter: Omar BENHAMID | Owner: (none)
Type: Bug | Status: new
Component: Forms | Version: 3.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Omar BENHAMID):

* version: 5.1 => 3.2


Old description:
New description:
--
--
Ticket URL: <https://code.djangoproject.com/ticket/35640#comment:1>

Django

unread,
Jul 30, 2024, 3:54:52 AM7/30/24
to django-...@googlegroups.com
#35640: ModelMultipleChoiceField fail when the foreignkey remote model is a child
class of anohter model
-------------------------------+--------------------------------------
Reporter: Omar BENHAMID | Owner: (none)
Type: Bug | Status: closed
Component: Forms | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by David Sanders):

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

Comment:

Sorry but 3.2 is no longer supported 🤷‍♂️
--
Ticket URL: <https://code.djangoproject.com/ticket/35640#comment:2>

Django

unread,
Aug 4, 2024, 6:31:15 AM8/4/24
to django-...@googlegroups.com
#35640: ModelMultipleChoiceField fail when the foreignkey remote model is a child
class of anohter model
-------------------------------+--------------------------------------
Reporter: Omar BENHAMID | Owner: (none)
Type: Bug | Status: closed
Component: Forms | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Comment (by Omar BENHAMID):

There is indeed an issue, the question is the use cas support it : is it
worth solving ?

Currently django-slick-report builds a search form using a
ModelMultipleChoiceField backed by a simple ForeignKey field (the aim is
to select mutliple values for an "OR-search"). The way values are genrated
for the choices of the widgets are the same only if the field is iterable
(so one to many but not many to one) and this often works well except when
model inheritence is used.

I can provide a test case but the question before is : is this usage
supported ? Shall we try to make this part more robust in django?
--
Ticket URL: <https://code.djangoproject.com/ticket/35640#comment:3>

Django

unread,
Aug 4, 2024, 6:50:15 AM8/4/24
to django-...@googlegroups.com
#35640: ModelMultipleChoiceField fail when the foreignkey remote model is a child
class of anohter model
-------------------------------+--------------------------------------
Reporter: Omar BENHAMID | Owner: (none)
Type: Bug | Status: closed
Component: Forms | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Comment (by David Sanders):

My advice would be to create a discussin on the Django forum in the
"Django Internals" category where other folks will be able to see the
issue: https://forum.djangoproject.com/c/internals/

We'll be able to gauge whether it's worth fixing by the responses people
give.
--
Ticket URL: <https://code.djangoproject.com/ticket/35640#comment:4>
Reply all
Reply to author
Forward
0 new messages