diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py index 4fa95f6f6e..cf8e7d3b86 100644 --- a/django/core/serializers/base.py +++ b/django/core/serializers/base.py @@ -108,7 +108,7 @@ class Serializer: else: if self.selected_fields is None or field.attname[:-3] in self.selected_fields: self.handle_fk_field(obj, field) - for field in concrete_model._meta.many_to_many: + for field in concrete_model._meta.local_many_to_many: if field.serialize: if self.selected_fields is None or field.attname in self.selected_fields: self.handle_m2m_field(obj, field) diff --git a/tests/serializers/models/multi_table.py b/tests/serializers/models/multi_table.py index e3a5e7fd5f..4b07880925 100644 --- a/tests/serializers/models/multi_table.py +++ b/tests/serializers/models/multi_table.py @@ -8,6 +8,7 @@ class ParentManager(models.Manager): class Parent(models.Model): parent_data = models.CharField(max_length=30, unique=True) + parent_m2m = models.ManyToManyField('self') objects = ParentManager() diff --git a/tests/serializers/tests.py b/tests/serializers/tests.py index 66fc2ef40c..222eee7e95 100644 --- a/tests/serializers/tests.py +++ b/tests/serializers/tests.py @@ -12,8 +12,9 @@ from django.test import SimpleTestCase, override_settings, skipUnlessDBFeature from django.test.utils import Approximate from .models import ( - Actor, Article, Author, AuthorProfile, BaseModel, Category, ComplexModel, - Movie, Player, ProxyBaseModel, ProxyProxyBaseModel, Score, Team, + Actor, Article, Author, AuthorProfile, BaseModel, Category, Child, + ComplexModel, Movie, Player, ProxyBaseModel, ProxyProxyBaseModel, Score, + Team, ) @@ -340,6 +341,13 @@ class SerializersTestBase: self.assertEqual(base_data, proxy_data.replace('proxy', '')) self.assertEqual(base_data, proxy_proxy_data.replace('proxy', '')) + def test_serialize_inherited_m2m(self): + child_1 = Child.objects.create(parent_data='a', child_data='b') + child_2 = Child.objects.create(parent_data='c', child_data='d') + child_1.parent_m2m.add(child_2) + child_data = serializers.serialize(self.serializer_name, [child_1, child_2]) + self.assertEqual(self._get_field_values(child_data, 'parent_m2m'), []) + class SerializerAPITests(SimpleTestCase):