﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35996	Missing chunk_size throws exception when serializing many-to-many Model field	Erica Pisani	Erica Pisani	"Related issue: https://code.djangoproject.com/ticket/35238

I'm currently upgrading a project from v4.x to v5.0.1 and, when attempting to save a model with a prefetched many-to-many field, the ""ValueError: chunk_size must be provided when using QuerySet.iterator() after prefetch_related()"" exception is thrown within the core Django [https://github.com/django/django/blob/9c436a09b3a641874881706495ae07293aa97c2f/django/core/serializers/python.py#L86 serializer]:


Here's the trace within my project that leads to the exception:
{{{
File "".../site-packages/django/db/models/base.py"", line 822, in save
    self.save_base(
  File "".../site-packages/django/db/models/base.py"", line 924, in save_base
    post_save.send(
  File "".../site-packages/django/dispatch/dispatcher.py"", line 189, in send
    response = receiver(signal=self, sender=sender, **named)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

# The revisions files below are referring to the django-reversion library

  File "".../site-packages/reversion/revisions.py"", line 340, in _post_save_receiver
    add_to_revision(instance, model_db=using)
  File "".../site-packages/reversion/revisions.py"", line 209, in add_to_revision
    _add_to_revision(obj, db, model_db, True)
  File "".../site-packages/reversion/revisions.py"", line 184, in _add_to_revision
    serialized_data=serializers.serialize(
                    ^^^^^^^^^^^^^^^^^^^^^^
  
File "".../site-packages/django/core/serializers/__init__.py"", line 134, in serialize
    s.serialize(queryset, **options)
  File "".../site-packages/django/core/serializers/base.py"", line 143, in serialize
    self.handle_m2m_field(obj, field)
  File "".../site-packages/django/core/serializers/python.py"", line 91, in handle_m2m_field
    queryset_iterator(obj, field),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "".../site-packages/django/core/serializers/python.py"", line 86, in queryset_iterator
    .iterator()
     ^^^^^^^^^^
  File "".../site-packages/django/db/models/query.py"", line 532, in iterator
    raise ValueError(
ValueError: chunk_size must be provided when using QuerySet.iterator() after prefetch_related().
}}}


I confirmed that applying the same fix as the issue linked above addresses the issue.

**Proposed solution:**
I'm happy to put up a pull request with the patch, and can emulate what the issue linked above did which is conditionally supply a `chunk_size` similar to the solution implemented in the issue linked above'

"	Bug	assigned	Core (Serialization)	5.0	Release blocker			Erica Pisani	Unreviewed	1	0	0	0	0	0
