Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ManyRelatedField.get_value clearing field on partial update #2475

Merged
merged 3 commits into from
Jan 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions rest_framework/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,12 @@ def get_value(self, dictionary):
# We override the default field access in order to support
# lists in HTML forms.
if html.is_html_input(dictionary):
# Don't return [] if the update is partial
if self.field_name not in dictionary:
if getattr(self.root, 'partial', False):
return empty
return dictionary.getlist(self.field_name)

return dictionary.get(self.field_name, empty)

def to_internal_value(self, data):
Expand Down
33 changes: 33 additions & 0 deletions tests/test_relations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .utils import mock_reverse, fail_reverse, BadType, MockObject, MockQueryset
from django.core.exceptions import ImproperlyConfigured
from django.utils.datastructures import MultiValueDict
from rest_framework import serializers
from rest_framework.fields import empty
from rest_framework.test import APISimpleTestCase
import pytest

Expand Down Expand Up @@ -134,3 +136,34 @@ def test_slug_related_lookup_invalid_type(self):
def test_representation(self):
representation = self.field.to_representation(self.instance)
assert representation == self.instance.name


class TestManyRelatedField(APISimpleTestCase):
def setUp(self):
self.instance = MockObject(pk=1, name='foo')
self.field = serializers.StringRelatedField(many=True)
self.field.field_name = 'foo'

def test_get_value_regular_dictionary_full(self):
assert 'bar' == self.field.get_value({'foo': 'bar'})
assert empty == self.field.get_value({'baz': 'bar'})

def test_get_value_regular_dictionary_partial(self):
setattr(self.field.root, 'partial', True)
assert 'bar' == self.field.get_value({'foo': 'bar'})
assert empty == self.field.get_value({'baz': 'bar'})

def test_get_value_multi_dictionary_full(self):
mvd = MultiValueDict({'foo': ['bar1', 'bar2']})
assert ['bar1', 'bar2'] == self.field.get_value(mvd)

mvd = MultiValueDict({'baz': ['bar1', 'bar2']})
assert [] == self.field.get_value(mvd)

def test_get_value_multi_dictionary_partial(self):
setattr(self.field.root, 'partial', True)
mvd = MultiValueDict({'foo': ['bar1', 'bar2']})
assert ['bar1', 'bar2'] == self.field.get_value(mvd)

mvd = MultiValueDict({'baz': ['bar1', 'bar2']})
assert empty == self.field.get_value(mvd)