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
73cd4e23
Commit
73cd4e23
authored
Mar 31, 2014
by
Xavier Ordoquy
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1477 from vlastv/patch-1
Writable star source with instance
parents
2a27674a
f5fc6937
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
10 deletions
+36
-10
rest_framework/serializers.py
+10
-10
rest_framework/tests/test_serializer.py
+26
-0
No files found.
rest_framework/serializers.py
View file @
73cd4e23
...
...
@@ -438,16 +438,6 @@ class BaseSerializer(WritableField):
raise
ValidationError
(
self
.
error_messages
[
'required'
])
return
# Set the serializer object if it exists
obj
=
get_component
(
self
.
parent
.
object
,
self
.
source
or
field_name
)
if
self
.
parent
.
object
else
None
# If we have a model manager or similar object then we need
# to iterate through each instance.
if
(
self
.
many
and
not
hasattr
(
obj
,
'__iter__'
)
and
is_simple_callable
(
getattr
(
obj
,
'all'
,
None
))):
obj
=
obj
.
all
()
if
self
.
source
==
'*'
:
if
value
:
reverted_data
=
self
.
restore_fields
(
value
,
{})
...
...
@@ -457,6 +447,16 @@ class BaseSerializer(WritableField):
if
value
in
(
None
,
''
):
into
[(
self
.
source
or
field_name
)]
=
None
else
:
# Set the serializer object if it exists
obj
=
get_component
(
self
.
parent
.
object
,
self
.
source
or
field_name
)
if
self
.
parent
.
object
else
None
# If we have a model manager or similar object then we need
# to iterate through each instance.
if
(
self
.
many
and
not
hasattr
(
obj
,
'__iter__'
)
and
is_simple_callable
(
getattr
(
obj
,
'all'
,
None
))):
obj
=
obj
.
all
()
kwargs
=
{
'instance'
:
obj
,
'data'
:
value
,
...
...
rest_framework/tests/test_serializer.py
View file @
73cd4e23
...
...
@@ -508,6 +508,32 @@ class ValidationTests(TestCase):
)
self
.
assertEqual
(
serializer
.
is_valid
(),
True
)
def
test_writable_star_source_on_nested_serializer_with_parent_object
(
self
):
class
TitleSerializer
(
serializers
.
Serializer
):
title
=
serializers
.
WritableField
(
source
=
'title'
)
class
AlbumSerializer
(
serializers
.
ModelSerializer
):
nested
=
TitleSerializer
(
source
=
'*'
)
class
Meta
:
model
=
Album
fields
=
(
'nested'
,)
class
PhotoSerializer
(
serializers
.
ModelSerializer
):
album
=
AlbumSerializer
(
source
=
'album'
)
class
Meta
:
model
=
Photo
fields
=
(
'album'
,
)
photo
=
Photo
(
album
=
Album
())
data
=
{
'album'
:
{
'nested'
:
{
'title'
:
'test'
}}}
serializer
=
PhotoSerializer
(
photo
,
data
=
data
)
self
.
assertEqual
(
serializer
.
is_valid
(),
True
)
self
.
assertEqual
(
serializer
.
data
,
data
)
def
test_writable_star_source_with_inner_source_fields
(
self
):
"""
Tests that a serializer with source="*" correctly expands the
...
...
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