Wednesday, October 31, 2018

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

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.

On Saturday, October 20, 2018 at 4:38:51 AM UTC-4, Carsten Fuchs wrote:
Can anyone help please?

Am 2018-10-11 um 16:58 schrieb Carsten Fuchs:
> 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
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8466e1c0-8e47-4e69-ae8b-9861014f9878%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment