From 79736e516a68f567c16e33e03f4e4f90f1029da0 Mon Sep 17 00:00:00 2001 From: Brian Grohe Date: Wed, 17 Jun 2015 13:18:50 -0400 Subject: [PATCH 1/3] Added failing test case Adding failing test case when many=false is explicitly defined https://github.com/tomchristie/django-rest-framework/issues/3042 --- tests/test_relations.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_relations.py b/tests/test_relations.py index a4815a06d..6c6543154 100644 --- a/tests/test_relations.py +++ b/tests/test_relations.py @@ -48,6 +48,11 @@ class TestPrimaryKeyRelatedField(APISimpleTestCase): representation = self.field.to_representation(self.instance) assert representation == self.instance.pk + def test_explicit_many_false(self): + field = serializers.PrimaryKeyRelatedField(queryset=self.queryset, many=False) + instance = field.to_internal_value(self.instance.pk) + assert instance is self.instance + class TestProxiedPrimaryKeyRelatedField(APISimpleTestCase): def setUp(self): From d24990ece26a51c65703c820d18897f7a4acfc23 Mon Sep 17 00:00:00 2001 From: Brian Grohe Date: Wed, 17 Jun 2015 13:43:35 -0400 Subject: [PATCH 2/3] Fixed many=False issue in related fields Added check to pop many from kwargs before passing to __init__ Fixed my lint issue from the previous commit --- rest_framework/relations.py | 2 ++ tests/test_relations.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 4f81dde34..e56fef5da 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -42,6 +42,8 @@ class RelatedField(Field): 'Relational fields should not provide a `queryset` argument, ' 'when setting read_only=`True`.' ) + if 'many' in kwargs: + kwargs.pop('many') super(RelatedField, self).__init__(**kwargs) def __new__(cls, *args, **kwargs): diff --git a/tests/test_relations.py b/tests/test_relations.py index 6c6543154..25a53f276 100644 --- a/tests/test_relations.py +++ b/tests/test_relations.py @@ -52,7 +52,7 @@ class TestPrimaryKeyRelatedField(APISimpleTestCase): field = serializers.PrimaryKeyRelatedField(queryset=self.queryset, many=False) instance = field.to_internal_value(self.instance.pk) assert instance is self.instance - + class TestProxiedPrimaryKeyRelatedField(APISimpleTestCase): def setUp(self): From 51cda112f584746fd1bedc4395df4754d2f82253 Mon Sep 17 00:00:00 2001 From: Brian Grohe Date: Fri, 19 Jun 2015 09:21:35 -0400 Subject: [PATCH 3/3] Simplified if statement to one line solution Based on feedback on the pull request, changed solution to be simpler for issue 3042 --- rest_framework/relations.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index e56fef5da..3024bccb8 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -42,8 +42,7 @@ class RelatedField(Field): 'Relational fields should not provide a `queryset` argument, ' 'when setting read_only=`True`.' ) - if 'many' in kwargs: - kwargs.pop('many') + kwargs.pop('many', None) super(RelatedField, self).__init__(**kwargs) def __new__(cls, *args, **kwargs):