#24272: Better error messages for prefetch_related
-------------------------------------+-------------------------------------
Reporter: Todor Velichkov | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related, | Triage Stage: Accepted
GenericRelation, |
related_query_name |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Todor Velichkov):
I did some debugging and I think I find out why `prefetch_related` on
`GenericRelation` using `related_query_name` is not working. i.e.
`TaggedItem.objects.filter(books__author__name='E L
James').prefetch_related('books')`
It starts from the
[https://github.com/django/django/blob/1.11.2/django/db/models/fields/related_descriptors.py#L596
get_prefetch_queryset] at `related_descriptors`, where `self.field` is a
`GenericRelation` field
(`<django.contrib.contenttypes.fields.GenericRelation: tags>` in this
example) the
[https://github.com/django/django/blob/1.11.2/django/contrib/contenttypes/fields.py#L276
GenericRelation] class inherits from `ForeignObject` but does not
implement
[https://github.com/django/django/blob/1.11.2/django/db/models/fields/related.py#L655
get_local_related_value] and `get_foreign_related_value` methods which
looks incompatible with the `GenericRelation` interface, because they
search for `object_id` attribute inside a `Book` model.
I would love to try to fix this, but I still can't fully understand the
code, I'm not even sure what needs to be returned here, all tags related
to this book maybe? If thats the case, then the code in
`get_prefetch_queryset` looks like its expected to be returned only a
single instance, not many, this is confusing me.
--
Ticket URL: <https://code.djangoproject.com/ticket/24272#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.81c8a2384ed633051115760aa52b8cc5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.