Opened 8 years ago

Closed 8 years ago

#18200 closed Bug (duplicate)

prefetch_related works invalid!

Reported by: Artem Skoretskiy Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4
Severity: Normal Keywords:
Cc: tonn81@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I found that getting GenericRelation related objects by prefetch_related gives false result!


from django.contrib.comments.models import Comment

class Media(models.Model):
    comments = generic.GenericRelation(Comment, object_id_field='object_pk')

>>> Media.objects.filter(pk=1731)[0].comments.all() # works as expected
[<Comment: admin: It is a new comment>]
>>> Media.objects.filter(pk=1731).prefetch_related('comments')[0].comments.all() # nothing found!

Thus ORM run proper SQL query:

FROM "django_comments"
WHERE ("django_comments"."object_pk" IN ('1731') AND "django_comments"."content_type_id" = 41 )

I may provide any information if needed. At the point I see no entry point to start digging.

Change History (2)

comment:1 Changed 8 years ago by Claude Paroz

Maybe the same issue as in #17991? Can you check?

comment:2 Changed 8 years ago by Artem Skoretskiy

Resolution: duplicate
Status: newclosed

The fix suggested in #17991 also works for me so it seems to be the same bug.

Note: See TracTickets for help on using tickets.
Back to Top