ManyToManyRel.get_related_field() doesn't account for defined through to_field
While investigating edge cases to_field_allowed
has to cope with I stumbled upon the following unusual scenario:
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 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.
Change History
(5)
Patch needs improvement: |
set
|
Triage Stage: |
Unreviewed → Accepted
|
Patch needs improvement: |
unset
|
Triage Stage: |
Accepted → Ready for checkin
|
Resolution: |
→ fixed
|
Status: |
new → closed
|
Updated patch.