question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
acarasimon96commented, Oct 28, 2019

It seems that I’ve discovered the culprit of this nasty bug.

Following @RadoRado’s example above, I’ve noticed that swapping out ParentalManyToManyField to models.ManyToManyField allowed the use of prefetch_related in exchange of losing the ability to see the not-yet-saved Actors objects in Wagtail previews. But with some debugging using pdb, I found out that DeferringRelatedManager in fields.create_deferring_forward_many_to_many_manager is missing a get_prefetch_queryset function, which the Django ORM needs when attempting to reference a ParentalManyToManyField field through prefetch_related – in this case, Movie.actors. ManyRelatedManager (in which DeferringRelatedManager 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)

3reactions
harrislapiroffcommented, Jun 10, 2020

Ran into this today! Would be very excited to see @chosak’s PR cross the finish line.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found