[Django] #23862: ManyToManyRel.get_related_field() doesn't account for defined through to_field

16 views
Skip to first unread message

Django

unread,
Nov 17, 2014, 5:43:35 PM11/17/14
to django-...@googlegroups.com
#23862: ManyToManyRel.get_related_field() doesn't account for defined through
to_field
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: charettes
Type: Bug | Status: new
Component: Database | Version: master
layer (models, ORM) | Keywords:
Severity: Normal | Has patch: 1
Triage Stage: | Needs tests: 0
Unreviewed | Easy pickings: 0
Needs documentation: 0 |
Patch needs improvement: 0 |
UI/UX: 0 |
-------------------------------------+-------------------------------------
While investigating edge cases `to_field_allowed` has to cope with I
stumbled upon the following unusual scenario:

{{{#!python
class Ingredient(models.Model):
iname = models.CharField(max_length=20, unique=True)

class Recipe(models.Model):
rname = models.CharField(max_length=20, unique=True)
ingredients = models.ManyToManyField(
Ingredient, through='RecipeIngredient', related_name='recipes'
)

class RecipeIngredient(models.Model):
ingredient = models.ForeignKey(Ingredient, to_field='iname')
recipe = models.ForeignKey(Recipe, to_field='rname')
}}}

Which is not correctly handled by the actual
`ManyToManyRel.get_related_field()` implementation that assumes
[https://github.com/django/django/blob/4b9eb7602d6d8756933d1a767004155a263cc150/django/db/models/fields/related.py#L1349-L1355
this is always the primary key on the target model].

Since `ManyToManyRel` is an implementation detail the provided test is
against `ManyToManyField.get_choices()` method which relies on it. The
patch also includes tests for related managers interaction since this
reference scenario was not tested in `m2m_through` yet.

The fact that the implementation doesn't actually return the correct field
means that we don't have to backport the required `to_field_allowed`
adjustments (which I'll submit as another ticket/patch to facilitate
backports of #23754 and #23839) since it never worked in previous Django
versions.

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

Django

unread,
Nov 18, 2014, 6:58:45 AM11/18/14
to django-...@googlegroups.com
#23862: ManyToManyRel.get_related_field() doesn't account for defined through
to_field
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: charettes
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 1
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by akaariai):

* needs_better_patch: 0 => 1
* stage: Unreviewed => Accepted


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

Django

unread,
Nov 18, 2014, 7:36:00 AM11/18/14
to django-...@googlegroups.com
#23862: ManyToManyRel.get_related_field() doesn't account for defined through
to_field
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: charettes
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0

Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by charettes):

* needs_better_patch: 1 => 0


Comment:

Updated patch.

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

Django

unread,
Nov 19, 2014, 1:04:23 AM11/19/14
to django-...@googlegroups.com
#23862: ManyToManyRel.get_related_field() doesn't account for defined through
to_field
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: charettes
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by akaariai):

* stage: Accepted => Ready for checkin


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

Django

unread,
Nov 21, 2014, 4:04:12 PM11/21/14
to django-...@googlegroups.com
#23862: ManyToManyRel.get_related_field() doesn't account for defined through
to_field
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: charettes
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) | Resolution: fixed

Severity: Normal | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | 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:"c7087bc777a078f71a0705d9d0eba51d2679a206"]:
{{{
#!CommitTicketReference repository=""
revision="c7087bc777a078f71a0705d9d0eba51d2679a206"
Fixed #23862 -- Made ManyToManyRel.get_related_field() respect to_field.
}}}

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

Django

unread,
Nov 25, 2014, 3:28:37 PM11/25/14
to django-...@googlegroups.com
#23862: ManyToManyRel.get_related_field() doesn't account for defined through
to_field
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: charettes
Type: Bug | Status: closed
Component: Database layer | Version: master
(models, ORM) | Resolution: fixed
Severity: Normal | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette <charette.s@…>):

In [changeset:"3a9aa155e2f7326df669953980ac87e78e932c43"]:
{{{
#!CommitTicketReference repository=""
revision="3a9aa155e2f7326df669953980ac87e78e932c43"
Fixed #23915 -- Made sure m2m fields through non-pk to_field are allowed
in the admin.

refs #23754, #23862
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23862#comment:5>

Reply all
Reply to author
Forward
0 new messages