Opened 2 years ago

Closed 2 years ago

#33950 closed Bug (duplicate)

ModelChoiceField and chained prefetch_related(): queries made twice

Reported by: Vincent Lefoulon Owned by: nobody
Component: Uncategorized Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Vincent Lefoulon)


I have these three models:

class Place(models.Model):
    name = models.CharField(max_length=255)

class Visit(models.Model):
    date = models.DateField()
    place = models.ForeignKey(Place, on_delete=models.CASCADE, related_name="visits")

class VisitDocument(models.Model):
    visit = models.ForeignKey(Visit, on_delete=models.CASCADE, related_name="documents")
    file = models.FileField()

A form to edit a visit:

class VisitForm(forms.ModelForm):
    class Meta:
        model = Visit
        exclude = ("place",)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.fields["documents_to_delete"] = forms.ModelMultipleChoiceField(

And a generic view for the places:

class PlaceView(DetailView):
    queryset = Place.objects.prefetch_related("visits__documents")

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        if "visit_forms" not in context:
            context["visit_forms"] = [
                for visit in self.object.visits.all()

    def post(self, request, *args, **kwargs):
        // Handle the form

Despite the prefetch_related("visits__documents") call in my view, ModelMultipleChoiceField() doesn't detect that the documents of the visit (i.e. the form instance) are already fetched because visit.documents.all()._prefetch_related_lookups is null:

So is in the view: self.object.visits.all()._prefetch_related_lookups is null as well.

Then the queries are made twice, erasing the prefetch_related() effect.

Many thanks!

Change History (3)

comment:1 by Vincent Lefoulon, 2 years ago

Description: modified (diff)

comment:2 by Vincent Lefoulon, 2 years ago

Description: modified (diff)

comment:3 by Carlton Gibson, 2 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #22841.

Note: See TracTickets for help on using tickets.
Back to Top