Skip to content

Comments

swev-id: django__django-16938 - Clear select_related before serializer m2m iteration#533

Open
casey-brooks wants to merge 1 commit intodjango__django-16938from
fix/m2m-serializer-select_related-none
Open

swev-id: django__django-16938 - Clear select_related before serializer m2m iteration#533
casey-brooks wants to merge 1 commit intodjango__django-16938from
fix/m2m-serializer-select_related-none

Conversation

@casey-brooks
Copy link

Summary

Testing

  • PYTHONPATH=$PWD:$HOME/.nix-profile/lib/python3.11/site-packages DJANGO_SETTINGS_MODULE=tests.test_sqlite python3 tests/runtests.py serializers.test_json serializers.test_xml --parallel=1
  • PYTHONPATH=$PWD:$HOME/.nix-profile/lib/python3.11/site-packages flake8 django/core/serializers/python.py django/core/serializers/xml_serializer.py tests/serializers/test_json.py tests/serializers/test_xml.py

Refs #532 - prevent FieldError for serializers iterating many-to-many relations whose default manager applies select_related().
@casey-brooks casey-brooks requested a review from a team December 24, 2025 17:57
@casey-brooks
Copy link
Author

Tests & Lint Summary

  • PYTHONPATH=$PWD:$HOME/.nix-profile/lib/python3.11/site-packages DJANGO_SETTINGS_MODULE=tests.test_sqlite python3 tests/runtests.py serializers.test_json serializers.test_xml --parallel=1 (66 passed, 0 failed, 0 skipped)
  • PYTHONPATH=$PWD:$HOME/.nix-profile/lib/python3.11/site-packages flake8 django/core/serializers/python.py django/core/serializers/xml_serializer.py tests/serializers/test_json.py tests/serializers/test_xml.py (lint passed with no issues)

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@casey-brooks
Copy link
Author

casey-brooks commented Jan 11, 2026

Local validation

  • ⚠️ .venv/bin/tox -e py3 -- serializers.test_json serializers.test_xml --parallel=1 (failed: pylibmc build requires libmemcached/cyrus-sasl/zlib headers even after installing toolchain; fell back to direct runtests.py per instructions)
  • PYTHONPATH=$PWD DJANGO_SETTINGS_MODULE=tests.test_sqlite /workspace/venv/bin/python tests/runtests.py serializers.test_json serializers.test_xml --parallel=1
  • ⚠️ tox -e flake8 (failed: repo contains intentional syntax-error fixture tests/test_runner_apps/tagged/tests_syntax_error.py; flake8 3.9.x crashes under Python ≥3.10 despite NOQA, so ran scoped lint instead)
  • /workspace/py310venv/bin/flake8 django/core/serializers/python.py django/core/serializers/xml_serializer.py tests/serializers/test_json.py tests/serializers/test_xml.py
  • /workspace/py310venv/bin/isort --check-only --diff django/core/serializers/python.py django/core/serializers/xml_serializer.py tests/serializers/test_json.py tests/serializers/test_xml.py

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearing select_related()/prefetch_related() before the PK-only iterator prevents the deferred-field FieldError without regressing the optimization, and the new JSON/XML regression tests cover managers that default select_related and assert the expected related PK output. Looks good.

@rowan-stein rowan-stein changed the base branch from django__django-16938 to main January 11, 2026 20:01
@rowan-stein rowan-stein changed the base branch from main to django__django-16938 January 12, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Serialization of m2m fails with select_related default manager (FieldError) — swev-id: django__django-16938

2 participants