{{{
class Model1(models.Model):
children = models.ManyToManyField('self', blank=True,
related_name='parents')
class Model2(models.Model):
children = models.ManyToManyField('self', blank=True,
related_name='parents')
}}}
{{{
migrations.CreateModel(
name='Model1',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True,
serialize=False, verbose_name='ID')),
('children', models.ManyToManyField(blank=True,
related_name='_model1_children_+', to='raftprot.Model1')),
],
),
migrations.CreateModel(
name='Model2',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True,
serialize=False, verbose_name='ID')),
('children', models.ManyToManyField(blank=True,
related_name='parents', to='raftprot.Model2')),
],
),
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28309>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Tim Graham):
I think there's a mistake in the ticket description. The two `children`
fields in the models are identical.
--
Ticket URL: <https://code.djangoproject.com/ticket/28309#comment:1>
Old description:
New description:
The related_name is not set correctly on ManyToManyField when it is
related with 'self'. When I use the model name explicitly, it is set
correctly.
{{{
class Model1(models.Model):
children = models.ManyToManyField('self', blank=True)
class Model2(models.Model):
children = models.ManyToManyField('self', blank=True,
related_name='parents')
}}}
{{{
migrations.CreateModel(
name='Model1',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True,
serialize=False, verbose_name='ID')),
('children', models.ManyToManyField(blank=True,
related_name='_model1_children_+', to='raftprot.Model1')),
],
),
migrations.CreateModel(
name='Model2',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True,
serialize=False, verbose_name='ID')),
('children', models.ManyToManyField(blank=True,
related_name='parents', to='raftprot.Model2')),
],
),
}}}
--
Comment (by Tim Graham):
I've edited the ticket description as I think you meant, please correct it
if needed. If the unexpected output in the migration is
`related_name='_model1_children_+'`, that's coming from
[https://github.com/django/django/blob/a6b5321ce997b899e540bb427cca98cb2b93a106/django/db/models/fields/related.py#L1531-L1546
ManyToManyField.contribute_to_class()]. I'm uncertain if this is a bug.
Can you please clarify?
--
Ticket URL: <https://code.djangoproject.com/ticket/28309#comment:2>
* status: new => closed
* resolution: => needsinfo
--
Ticket URL: <https://code.djangoproject.com/ticket/28309#comment:3>