Opened 5 months ago
Closed 5 months ago
#35654 closed Bug (needsinfo)
SystemCheckError on GenericRelation with lazy reference
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 )
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 (3)
comment:1 by , 5 months ago
Description: | modified (diff) |
---|
comment:2 by , 5 months ago
comment:3 by , 5 months ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
Closing until a new example is available
Ah I'm silly here, the "TagedItem" model above has a typo, hence the error. Please ignore this and I will try to replicate my initial problem with another example.