Fixed #28725 -- Prevented serializing inherited ManyToManyFields in child model.

This commit is contained in:
Nadège Michel 2019-06-27 09:47:13 +02:00 committed by Mariusz Felisiak
parent 4305fbe8b1
commit 895b15771e
3 changed files with 12 additions and 3 deletions

View File

@ -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)

View File

@ -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()

View File

@ -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):