Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
6d89430d
Commit
6d89430d
authored
Jan 28, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2475 from sdreher/master
ManyRelatedField.get_value clearing field on partial update
parents
81c2562e
e7da266a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
0 deletions
+38
-0
rest_framework/relations.py
+5
-0
tests/test_relations.py
+33
-0
No files found.
rest_framework/relations.py
View file @
6d89430d
...
@@ -338,7 +338,12 @@ class ManyRelatedField(Field):
...
@@ -338,7 +338,12 @@ class ManyRelatedField(Field):
# We override the default field access in order to support
# We override the default field access in order to support
# lists in HTML forms.
# lists in HTML forms.
if
html
.
is_html_input
(
dictionary
):
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
.
getlist
(
self
.
field_name
)
return
dictionary
.
get
(
self
.
field_name
,
empty
)
return
dictionary
.
get
(
self
.
field_name
,
empty
)
def
to_internal_value
(
self
,
data
):
def
to_internal_value
(
self
,
data
):
...
...
tests/test_relations.py
View file @
6d89430d
from
.utils
import
mock_reverse
,
fail_reverse
,
BadType
,
MockObject
,
MockQueryset
from
.utils
import
mock_reverse
,
fail_reverse
,
BadType
,
MockObject
,
MockQueryset
from
django.core.exceptions
import
ImproperlyConfigured
from
django.core.exceptions
import
ImproperlyConfigured
from
django.utils.datastructures
import
MultiValueDict
from
rest_framework
import
serializers
from
rest_framework
import
serializers
from
rest_framework.fields
import
empty
from
rest_framework.test
import
APISimpleTestCase
from
rest_framework.test
import
APISimpleTestCase
import
pytest
import
pytest
...
@@ -134,3 +136,34 @@ class TestSlugRelatedField(APISimpleTestCase):
...
@@ -134,3 +136,34 @@ class TestSlugRelatedField(APISimpleTestCase):
def
test_representation
(
self
):
def
test_representation
(
self
):
representation
=
self
.
field
.
to_representation
(
self
.
instance
)
representation
=
self
.
field
.
to_representation
(
self
.
instance
)
assert
representation
==
self
.
instance
.
name
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment