Opened 7 years ago

Last modified 5 years ago

#29078 closed Cleanup/optimization

Serializer handle_m2m_field should honour any previous prefetch — at Initial Version

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

Description

Example:

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

groups = Group.objects.prefetch_related('permissionscontent_type').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 77 replaces iterator() with all()

self._current[field.name] = [

m2m_value(related) for related in getattr(obj, field.name).iterator()

]

Change History (0)

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