Opened 13 months ago
Last modified 13 months ago
#35996 closed Bug
Missing chunk_size throws exception when serializing many-to-many Model field — at Initial Version
| Reported by: | Erica Pisani | Owned by: | Erica Pisani |
|---|---|---|---|
| Component: | Core (Serialization) | Version: | 5.0 |
| Severity: | Normal | Keywords: | |
| Cc: | Erica Pisani | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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 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'