{{{#!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.
* needs_better_patch: 0 => 1
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/23862#comment:1>
* needs_better_patch: 1 => 0
Comment:
Updated patch.
--
Ticket URL: <https://code.djangoproject.com/ticket/23862#comment:2>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/23862#comment:3>
* 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>
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>