Skip to content

GenericPrefetch does not work correctly with Polymorphic Models #613

Open
@vladjkeeee13

Description

@vladjkeeee13

Description

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

  1. Create a polymorphic model with a custom manager.
  2. Use GenericPrefetch to prefetch related objects.
  3. 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions