Prepopulate blog in b.entry_set.all()[i].blog ?

25 views
Skip to first unread message

Carsten Fuchs

unread,
Oct 11, 2018, 2:26:27 PM10/11/18
to django...@googlegroups.com
Dear Django group,

with Django 1.11.15, using the example models Blog and Entry at
<https://docs.djangoproject.com/en/2.1/topics/db/queries/#related-objects> for
reference, I have code like this:

b = Blog.objects.get(name="...")
for e in b.entry_set.all():
print(e.blog)

Obviously, e.blog == b, but I found that for each e, the access to e.blog causes
a subquery to fetch the blog object.

While I understand the concepts of select_related() and prefetch_related(), I
was surprised that the e.blog attributes are not prepopulated with b. Why is
that and what is the proper way to fix the problem?

Best regards,
Carsten

Carsten Fuchs

unread,
Oct 20, 2018, 4:38:51 AM10/20/18
to django...@googlegroups.com
Can anyone help please?

Tim Graham

unread,
Oct 31, 2018, 4:14:22 PM10/31/18
to Django users
It seems to be a bug as reported in https://code.djangoproject.com/ticket/29908. The ForeignKey must use to_field to reproduce the problem.
Reply all
Reply to author
Forward
0 new messages