Opened 4 months ago

Last modified 4 months ago

#35654 closed Bug

SystemCheckError on GenericRelation with lazy reference — at Version 1

Reported by: Giannis Terzopoulos Owned by:
Component: Database layer (models, ORM) Version: 5.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Giannis Terzopoulos)

Let's consider having 2 apps, core and bookmarks. Then I add a new TaggedItem model to core:

class TaggedItem(models.Model):
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey()

and a new Bookmark model to bookmarks:

class Bookmark(models.Model):
    tags = GenericRelation('core.TagedItem')

Running makemigrations here I am getting:

SystemCheckError: System check identified some issues:

ERRORS:
<function GenericRelation.contribute_to_class.<locals>.make_generic_foreign_order_accessors at 0x7a08cdb98e00>: (models.E022) <function GenericRelation.contribute_to_class.<locals>.make_generic_foreign_order_accessors at 0x7a08cdb98e00> contains a lazy reference to core.tageditem, but app 'core' doesn't provide model 'tageditem'.
bookmarks.Bookmark.tags: (fields.E307) The field bookmarks.Bookmark.tags was declared with a lazy reference to 'core.tageditem', but app 'core' doesn't provide model 'tageditem'.

The same happens if I makemigrations after adding TaggedItem, then add Bookmark and run makemigrations again.
It doesn't happen if I migrate for TaggedItem, then add Bookmark and makemigrations.

It also doesn't happen if I import TaggedItem and use that in GenericRelation:

from core.models import TaggedItem


class Bookmark(models.Model):
    tags = GenericRelation(TaggedItem)

The downside of this in my initial use case was that I would have to work around circular import errors, which wasn't easily doable.

Finally, having both models in the same app/models.py and using the lazy reference GenericRelation doesn't trigger the error either. So it seems to me as if something is missing here.

Change History (1)

comment:1 by Giannis Terzopoulos, 4 months ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top