Merge pull request #2975 from sheppard/read-only-nested

can't create nested serializers for unique_together relations
This commit is contained in:
Xavier Ordoquy 2015-06-01 12:01:40 +01:00
commit e3f856ff1d
2 changed files with 37 additions and 0 deletions

View File

@ -1088,6 +1088,9 @@ class ModelSerializer(Serializer):
if extra_kwargs.get('default') and kwargs.get('required') is False: if extra_kwargs.get('default') and kwargs.get('required') is False:
kwargs.pop('required') kwargs.pop('required')
if kwargs.get('read_only', False):
extra_kwargs.pop('required', None)
kwargs.update(extra_kwargs) kwargs.update(extra_kwargs)
return kwargs return kwargs

View File

@ -316,6 +316,14 @@ class RelationalModel(models.Model):
through = models.ManyToManyField(ThroughTargetModel, through=Supplementary, related_name='reverse_through') through = models.ManyToManyField(ThroughTargetModel, through=Supplementary, related_name='reverse_through')
class UniqueTogetherModel(models.Model):
foreign_key = models.ForeignKey(ForeignKeyTargetModel, related_name='unique_foreign_key')
one_to_one = models.OneToOneField(OneToOneTargetModel, related_name='unique_one_to_one')
class Meta:
unique_together = ("foreign_key", "one_to_one")
class TestRelationalFieldMappings(TestCase): class TestRelationalFieldMappings(TestCase):
def test_pk_relations(self): def test_pk_relations(self):
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
@ -395,6 +403,32 @@ class TestRelationalFieldMappings(TestCase):
""") """)
self.assertEqual(unicode_repr(TestSerializer()), expected) self.assertEqual(unicode_repr(TestSerializer()), expected)
def test_nested_unique_together_relations(self):
class TestSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = UniqueTogetherModel
depth = 1
expected = dedent("""
TestSerializer():
url = HyperlinkedIdentityField(view_name='uniquetogethermodel-detail')
foreign_key = NestedSerializer(read_only=True):
url = HyperlinkedIdentityField(view_name='foreignkeytargetmodel-detail')
name = CharField(max_length=100)
one_to_one = NestedSerializer(read_only=True):
url = HyperlinkedIdentityField(view_name='onetoonetargetmodel-detail')
name = CharField(max_length=100)
class Meta:
validators = [<UniqueTogetherValidator(queryset=UniqueTogetherModel.objects.all(), fields=('foreign_key', 'one_to_one'))>]
""")
if six.PY2:
# This case is also too awkward to resolve fully across both py2
# and py3. (See above)
expected = expected.replace(
"('foreign_key', 'one_to_one')",
"(u'foreign_key', u'one_to_one')"
)
self.assertEqual(unicode_repr(TestSerializer()), expected)
def test_pk_reverse_foreign_key(self): def test_pk_reverse_foreign_key(self):
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
class Meta: class Meta: