Opened 7 months ago

Last modified 6 months ago

#29078 assigned Cleanup/optimization

Serializer handle_m2m_field() should honor any previous prefetch

Reported by: xx396 Owned by: Zeeshan Khan
Component: Core (Serialization) Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by xx396)

Example:

from django.contrib.auth.models import Group
from django.core import serializers

groups = Group.objects.prefetch_related('permissions').all()
serializers.serialize('json', groups)

This will N+1 query the permissions as handle_m2m_field uses iterator() which bypasses any cache. Suggest serializers/python.py line 78 replaces iterator() with all()

Change History (4)

comment:1 Changed 7 months ago by xx396

Description: modified (diff)

comment:2 Changed 7 months ago by xx396

Description: modified (diff)

comment:3 Changed 6 months ago by Zeeshan Khan

Owner: changed from nobody to Zeeshan Khan
Status: newassigned

comment:4 Changed 6 months ago by Tim Graham

Easy pickings: unset
Summary: Serializer handle_m2m_field should honour any previous prefetchSerializer handle_m2m_field() should honor any previous prefetch
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

I guess it would be nice to fix that but removing iterator() negates the memory savings on the non-prefetch case, doesn't it?

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