Prefetch related not working for parental many to many relations
See original GitHub issue👋
I want to apologize in advance, if I’m missing something obvious, but having the following example:
from django.db import models
from modelcluster.models import ClusterableModel
from modelcluster.fields import ParentalManyToManyField
class Movie(ClusterableModel):
name = models.CharField(max_length=250)
actors = ParentalManyToManyField('Actors', related_name='movies')
class Actors(models.Model):
name = models.CharField(max_length=250)
When I do
Movie.objects.prefetch_related('actors')
I get the following error:
ValueError: 'actors' does not resolve to an item that supports prefetching - this is an invalid parameter to prefetch_related().
The question is - how am I supposed to do the prefetch_related
on ParentalManyToManyField
?
The context is Wagtail, but I’m posting this here, since the issues seems to be from the clusterable model.
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:8
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Django prefetch_related from foreignkey with ... - Stack Overflow
This allows it to prefetch many-to-many and many-to-one objects, which cannot be done using select_related, in addition to the foreign key and one-to-one...
Read more >QuerySetProxy - ormar - GitHub Pages
Querying related models from ManyToMany cleans list of related models loaded on parent model: Example: post.categories.first() will set post.categories to ...
Read more >Relationship Loading Techniques
So if eager loading many levels deep, “selectin” loading still will not require any JOINs for simple one-to-many or many-to-one relationships.
Read more >Prefetch_related and select_related functions in django
As we can see, prefetch is implemented using the IN statement. In this way, when there are too many objects in QuerySet, performance...
Read more >Automatic prefetching in querysets - Google Groups
The 'Default' example above is a classic example of the N+1 query problem, a problem that is widespread in Django apps. This pattern...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It seems that I’ve discovered the culprit of this nasty bug.
Following @RadoRado’s example above, I’ve noticed that swapping out
ParentalManyToManyField
tomodels.ManyToManyField
allowed the use ofprefetch_related
in exchange of losing the ability to see the not-yet-savedActors
objects in Wagtail previews. But with some debugging usingpdb
, I found out thatDeferringRelatedManager
infields.create_deferring_forward_many_to_many_manager
is missing aget_prefetch_queryset
function, which the Django ORM needs when attempting to reference aParentalManyToManyField
field throughprefetch_related
– in this case,Movie.actors
.ManyRelatedManager
(in whichDeferringRelatedManager
is based from I think) does have that missing function.So you may be wondering at this point where in the Django source code does this check happen. It turns out that occurs in
django.db.models.query.get_prefetcher
(specifically line 1700 as of Django 2.2)Ran into this today! Would be very excited to see @chosak’s PR cross the finish line.