From 805a915e7c2c0122ea588fa0dcac9f92e3276bbc Mon Sep 17 00:00:00 2001 From: "S. Andrew Sheppard" Date: Wed, 27 May 2015 21:06:57 -0500 Subject: [PATCH 1/4] can't nest unique_together relations --- tests/test_model_serializer.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index dc34649ea..048a12fba 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -316,6 +316,13 @@ class RelationalModel(models.Model): 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): def test_pk_relations(self): class TestSerializer(serializers.ModelSerializer): @@ -395,6 +402,25 @@ class TestRelationalFieldMappings(TestCase): """) 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 = [] + """) + self.assertEqual(unicode_repr(TestSerializer()), expected) + def test_pk_reverse_foreign_key(self): class TestSerializer(serializers.ModelSerializer): class Meta: From 8c7b5fc5c132ee4648aef7e95868dec9064d8ef6 Mon Sep 17 00:00:00 2001 From: "S. Andrew Sheppard" Date: Wed, 27 May 2015 21:14:08 -0500 Subject: [PATCH 2/4] pop required extra_kwargs if read_only is set --- rest_framework/serializers.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 8e1e50bc7..b1d58ee5f 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -1088,6 +1088,9 @@ class ModelSerializer(Serializer): if extra_kwargs.get('default') and kwargs.get('required') is False: kwargs.pop('required') + if kwargs.get('read_only', False): + extra_kwargs.pop('required', None) + kwargs.update(extra_kwargs) return kwargs From 0b8b288be597bd4d93d30c74f7c017a9d8abf497 Mon Sep 17 00:00:00 2001 From: "S. Andrew Sheppard" Date: Thu, 28 May 2015 08:20:43 -0500 Subject: [PATCH 3/4] python2 compat --- tests/test_model_serializer.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index 048a12fba..8ff97b766 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -419,6 +419,13 @@ class TestRelationalFieldMappings(TestCase): class Meta: validators = [] """) + 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): From 4a3c844b7fceb68af86025b5f09ac24f5dffed6a Mon Sep 17 00:00:00 2001 From: "S. Andrew Sheppard" Date: Thu, 28 May 2015 08:29:15 -0500 Subject: [PATCH 4/4] flake8 --- tests/test_model_serializer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index 8ff97b766..49ad8ac10 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -319,6 +319,7 @@ class RelationalModel(models.Model): 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")