Skip to content

Commit de08f28

Browse files
authored
Test one to one with inheritance (encode#4575)
1 parent 8d0a91b commit de08f28

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from __future__ import unicode_literals
2+
3+
from django.db import models
4+
from django.test import TestCase
5+
6+
from rest_framework import serializers
7+
from tests.models import RESTFrameworkModel
8+
9+
10+
# Models
11+
from tests.test_multitable_inheritance import ChildModel
12+
13+
14+
# Regression test for #4290
15+
16+
class ChildAssociatedModel(RESTFrameworkModel):
17+
child_model = models.OneToOneField(ChildModel)
18+
child_name = models.CharField(max_length=100)
19+
20+
21+
# Serializers
22+
class DerivedModelSerializer(serializers.ModelSerializer):
23+
class Meta:
24+
model = ChildModel
25+
fields = ['id', 'name1', 'name2', 'childassociatedmodel']
26+
27+
28+
class ChildAssociatedModelSerializer(serializers.ModelSerializer):
29+
30+
class Meta:
31+
model = ChildAssociatedModel
32+
fields = ['id', 'child_name']
33+
34+
35+
# Tests
36+
class InheritedModelSerializationTests(TestCase):
37+
38+
def test_multitable_inherited_model_fields_as_expected(self):
39+
"""
40+
Assert that the parent pointer field is not included in the fields
41+
serialized fields
42+
"""
43+
child = ChildModel(name1='parent name', name2='child name')
44+
serializer = DerivedModelSerializer(child)
45+
self.assertEqual(set(serializer.data.keys()),
46+
set(['name1', 'name2', 'id', 'childassociatedmodel']))

0 commit comments

Comments
 (0)