[Django] #35654: SystemCheckError on GenericRelation with lazy reference

17 views
Skip to first unread message

Django

unread,
Aug 1, 2024, 3:08:20 PM8/1/24
to django-...@googlegroups.com
#35654: SystemCheckError on GenericRelation with lazy reference
-------------------------------------+-------------------------------------
Reporter: Giannis Terzopoulos | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.0 | 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
-------------------------------------+-------------------------------------
Let's consider having 2 apps, `core` and `bookmarks`. Then I add a new
`TaggetItem` model to `core`:
{{{#!python
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`:
{{{#!python
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`:
{{{#!python
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 doesn't trigger the
error either, so it seems to be as if something is missing here.
--
Ticket URL: <https://code.djangoproject.com/ticket/35654>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 1, 2024, 3:10:52 PM8/1/24
to django-...@googlegroups.com
#35654: SystemCheckError on GenericRelation with lazy reference
-------------------------------------+-------------------------------------
Reporter: Giannis Terzopoulos | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Giannis Terzopoulos:

Old description:
New description:

Let's consider having 2 apps, `core` and `bookmarks`. Then I add a new
`TaggedItem` model to `core`:
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.

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

Django

unread,
Aug 1, 2024, 3:24:14 PM8/1/24
to django-...@googlegroups.com
#35654: SystemCheckError on GenericRelation with lazy reference
-------------------------------------+-------------------------------------
Reporter: Giannis Terzopoulos | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Giannis Terzopoulos):

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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35654#comment:2>

Django

unread,
Aug 2, 2024, 2:09:58 AM8/2/24
to django-...@googlegroups.com
#35654: SystemCheckError on GenericRelation with lazy reference
-------------------------------------+-------------------------------------
Reporter: Giannis Terzopoulos | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: needsinfo
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 Sarah Boyce):

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

Comment:

Closing until a new example is available
--
Ticket URL: <https://code.djangoproject.com/ticket/35654#comment:3>
Reply all
Reply to author
Forward
0 new messages