Regression in aa4acc164d1247c0de515c959f7b09648b57dc42 (see #29899).
--
Ticket URL: <https://code.djangoproject.com/ticket/32676>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* component: Database layer (models, ORM) => Migrations
--
Ticket URL: <https://code.djangoproject.com/ticket/32676#comment:1>
Comment (by David Wobrock):
Hi,
Similarly to #32675, on a first and very quick look, the `ManyToManyField`
has some logic at
`django.db.models.fields.related.ManyToManyField.contribute_to_class` that
looks very much like the names described in the ticket itself:
{{{
def contribute_to_class(self, cls, name, **kwargs):
# To support multiple relations to self, it's useful to have a
non-None
# related name on symmetrical relations for internal reasons. The
# concept doesn't make a lot of sense externally ("you want me to
# specify *what* on my non-reversible relation?!"), so we set it
up
# automatically. The funky name reduces the chance of an
accidental
# clash.
if self.remote_field.symmetrical and (
self.remote_field.model == RECURSIVE_RELATIONSHIP_CONSTANT or
self.remote_field.model == cls._meta.object_name
):
self.remote_field.related_name = "%s_rel_+" % name
elif self.remote_field.is_hidden():
# If the backwards relation is disabled, replace the original
# related_name with one generated from the m2m field name.
Django
# still uses backwards relations internally and we need to
avoid
# clashes between multiple m2m fields with related_name ==
'+'.
self.remote_field.related_name = '_%s_%s_%s_+' % (
cls._meta.app_label,
cls.__name__.lower(),
name,
)
}}}
Since `_meta` should not be available anymore, I expect that the result
of the `self.remote_field.model == cls._meta.object_name` condition
changed, because we should not be able to use `cls._meta.app_label`.
I'm open to help if we have an idea about how we can fix this :)
--
Ticket URL: <https://code.djangoproject.com/ticket/32676#comment:2>
* stage: Unreviewed => Accepted
Comment:
That seems like a flaw of `RelatedField.deconstruct` tbh, it should not
include `related_name` and friends if it was not specified at
initialization.
I suggest we make `RelatedField.__init__` store `related_name` and
`related_query_name` as `self._related_name` and
`self._related_query_name` and use them instead of relying on
`self.remote_field` in `deconstruct`. That's the approach we've taken for
post-initialization alterable attributes in `Field`
[https://github.com/django/django/blob/4e5bbb6ef2287126badd32842b239f4a8a7394ca/django/db/models/fields/__init__.py#L169
for example].
--
Ticket URL: <https://code.djangoproject.com/ticket/32676#comment:3>
* owner: nobody => David Wobrock
* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* status: new => assigned
* needs_tests: 0 => 1
Comment:
Started working on a patch https://github.com/django/django/pull/14324
but still Work In Progress
--
Ticket URL: <https://code.djangoproject.com/ticket/32676#comment:4>
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
Comment:
Some discussions occurred on the Pull Request:
https://github.com/django/django/pull/14324#issuecomment-843038110
Added tests and updated release note, patch is ready for review :) Thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/32676#comment:5>
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32676#comment:6>
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/32676#comment:7>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/32676#comment:8>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"b9df2b74b98b4d63933e8061d3cfc1f6f39eb747" b9df2b7]:
{{{
#!CommitTicketReference repository=""
revision="b9df2b74b98b4d63933e8061d3cfc1f6f39eb747"
Fixed #32676 -- Prevented migrations from rendering related field
attributes when not passed during initialization.
Thanks Simon Charette for the implementation idea.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32676#comment:9>