[Django] #32851: GenericRelation inherited from Abstract model in different app fails to generate migration

7 views
Skip to first unread message

Django

unread,
Jun 15, 2021, 12:54:15 PM6/15/21
to django-...@googlegroups.com
#32851: GenericRelation inherited from Abstract model in different app fails to
generate migration
-----------------------------------------+------------------------
Reporter: foucdeg | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 2.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
I'm trying to create a new model in a new app B, inheriting from an
abstract model in app A:

{{{
# app_b/models.py
from app_a.models import Foo

class ConcreteModel(Foo):
pass
}}}

`Foo` has a GenericRelation to another model of app A:

{{{
# app_a/models.py
from django.contrib.contenttypes.fields import GenericRelation

class Foo(models.Model):
class Meta:
abstract = True

some_relation = GenericRelation(
"Bar",
content_type_field="foo_type",
object_id_field="foo_id"
)

class Bar(models.Model):
# whatever
}}}

Generation of the initial migration of app B fails with the following
SystemCheck errors:

{{{
<function
GenericRelation.contribute_to_class.<locals>.make_generic_foreign_order_accessors
at 0x7fa2a6747c80>: (models.E022) <function
GenericRelation.contribute_to_class.<locals>.make_generic_foreign_order_accessors
at 0x7fa2a6747c80> contains a lazy reference to app_b.bar, but app 'app_b'
doesn't provide model 'bar'.
app_b.ConcreteModel.some_relation: (fields.E307) The field
app_b.ConcreteModel.some_relation was declared with a lazy reference to
'app_b.bar', but app 'app_b' doesn't provide model 'bar'.
}}}

Indeed, app_b does not provide Bar, app_a does.

I tried explicitly mentioning app_a in the GenericRelation:

{{{
some_relation = GenericRelation(
"app_a.Bar",
content_type_field="foo_type",
object_id_field="foo_id"
)
}}}

but this does not generate a migration for app_a, nor does it fix the
problem.

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

Django

unread,
Jun 16, 2021, 3:56:20 AM6/16/21
to django-...@googlegroups.com
#32851: GenericRelation inherited from Abstract model in different app fails to
generate migration
------------------------------------+--------------------------------------
Reporter: Foucauld Degeorges | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 2.2
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => invalid


Comment:

[https://docs.djangoproject.com/en/3.2/ref/models/fields/#foreignkey From
the ForeignKey docs]:

> To refer to models defined in another application, you can explicitly
specify a model with the full application label.

So, despite you saying you tried this, this works:

{{{
some_relation = GenericRelation(
"app_a.Bar",
content_type_field="foo_type",
object_id_field="foo_id"
)
}}}

As does making `Bar` a non-lazy reference:

{{{
some_relation = GenericRelation(
Bar,


content_type_field="foo_type",
object_id_field="foo_id"
)
}}}

Both of these approaches correctly generate the correct migrations. Please
see TicketClosingReasons/UseSupportChannels if you require further
assistance.

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

Reply all
Reply to author
Forward
0 new messages