You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am encountering an issue when using GenericPrefetch with PolymorphicModel. The content_object attribute is None when trying to prefetch related objects.
Environment
Django version: 5.0.7
django-polymorphic version: 3.1.0
Python version: 3.10
Steps to Reproduce
Create a polymorphic model with a custom manager.
Use GenericPrefetch to prefetch related objects.
Iterate over the related objects to access the content_object.
Code Example
# models.py
from django.db import models
from polymorphic.models import PolymorphicModel
from polymorphic.manager import PolymorphicManager
class Bookmark(PolymorphicModel):
url = models.URLField()
tags = GenericRelation(TaggedItem)
objects = CustomManager()
class TaggedItem(models.Model):
tag = models.SlugField()
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey("content_type", "object_id")
# custom_manager.py
from polymorphic.query import PolymorphicQuerySet
class CustomManager(PolymorphicManager):
def get_queryset(self) -> PolymorphicQuerySet:
return (
super()
.get_queryset()
.annotate(...)
)
# views.py
from django.db.models import Prefetch
from polymorphic.query import PolymorphicQuerySet
def get_queryset():
return TaggedItem.objects.prefetch_related(
GenericPrefetch(
lookup='content_object',
querysets=[
Bookmark.objects.all(),
],
),
)
# Accessing the `content_object`
for tag in tags:
print(tag.content_object) # This is None
Expected Behavior
The content_object should be correctly populated when using GenericPrefetch.
Actual Behavior
The content_object is None when using GenericPrefetch.
Additional Information
If GenericPrefetch is removed, the content_object is correctly populated. Additionally, if PolymorphicModel and PolymorphicManager are removed from the model, the issue does not occur. It seems that the combination of PolymorphicModel and GenericPrefetch might be causing the issue.
Request
Could you provide guidance on how to correctly use GenericPrefetch with PolymorphicModel or suggest any workarounds? Thank you!
The text was updated successfully, but these errors were encountered:
I would also love a proper implementation for this though, as my workaround requires separately handling each polymorphic model(-tree) that might be in the generic relation (which is exactly what generic relations are meant to avoid).
Description
I am encountering an issue when using
GenericPrefetch
withPolymorphicModel
. Thecontent_object
attribute isNone
when trying to prefetch related objects.Environment
Steps to Reproduce
GenericPrefetch
to prefetch related objects.content_object
.Code Example
Expected Behavior
The
content_object
should be correctly populated when usingGenericPrefetch
.Actual Behavior
The
content_object
isNone
when usingGenericPrefetch
.Additional Information
If
GenericPrefetch
is removed, thecontent_object
is correctly populated. Additionally, ifPolymorphicModel
andPolymorphicManager
are removed from the model, the issue does not occur. It seems that the combination ofPolymorphicModel
andGenericPrefetch
might be causing the issue.Request
Could you provide guidance on how to correctly use
GenericPrefetch
withPolymorphicModel
or suggest any workarounds? Thank you!The text was updated successfully, but these errors were encountered: