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
a985fc52
Commit
a985fc52
authored
Sep 09, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3364 from linovia/bug/3361
Don't pass `allow_empty` to `ListSerializer`'s children.
parents
b57e9cf4
aa481826
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
5 deletions
+52
-5
rest_framework/serializers.py
+6
-1
tests/test_serializer_nested.py
+46
-4
No files found.
rest_framework/serializers.py
View file @
a985fc52
...
@@ -113,8 +113,13 @@ class BaseSerializer(Field):
...
@@ -113,8 +113,13 @@ class BaseSerializer(Field):
kwargs['child'] = cls()
kwargs['child'] = cls()
return CustomListSerializer(*args, **kwargs)
return CustomListSerializer(*args, **kwargs)
"""
"""
allow_empty
=
kwargs
.
pop
(
'allow_empty'
,
None
)
child_serializer
=
cls
(
*
args
,
**
kwargs
)
child_serializer
=
cls
(
*
args
,
**
kwargs
)
list_kwargs
=
{
'child'
:
child_serializer
}
list_kwargs
=
{
'child'
:
child_serializer
,
}
if
allow_empty
is
not
None
:
list_kwargs
[
'allow_empty'
]
=
allow_empty
list_kwargs
.
update
(
dict
([
list_kwargs
.
update
(
dict
([
(
key
,
value
)
for
key
,
value
in
kwargs
.
items
()
(
key
,
value
)
for
key
,
value
in
kwargs
.
items
()
if
key
in
LIST_SERIALIZER_KWARGS
if
key
in
LIST_SERIALIZER_KWARGS
...
...
tests/test_serializer_nested.py
View file @
a985fc52
...
@@ -79,17 +79,23 @@ class TestNestedSerializerWithMany:
...
@@ -79,17 +79,23 @@ class TestNestedSerializerWithMany:
class
TestSerializer
(
serializers
.
Serializer
):
class
TestSerializer
(
serializers
.
Serializer
):
allow_null
=
NestedSerializer
(
many
=
True
,
allow_null
=
True
)
allow_null
=
NestedSerializer
(
many
=
True
,
allow_null
=
True
)
not_allow_null
=
NestedSerializer
(
many
=
True
)
not_allow_null
=
NestedSerializer
(
many
=
True
)
allow_empty
=
NestedSerializer
(
many
=
True
,
allow_empty
=
True
)
not_allow_empty
=
NestedSerializer
(
many
=
True
,
allow_empty
=
False
)
self
.
Serializer
=
TestSerializer
self
.
Serializer
=
TestSerializer
def
test_null_allowed_if_allow_null_is_set
(
self
):
def
test_null_allowed_if_allow_null_is_set
(
self
):
input_data
=
{
input_data
=
{
'allow_null'
:
None
,
'allow_null'
:
None
,
'not_allow_null'
:
[{
'example'
:
'2'
},
{
'example'
:
'3'
}]
'not_allow_null'
:
[{
'example'
:
'2'
},
{
'example'
:
'3'
}],
'allow_empty'
:
[{
'example'
:
'2'
}],
'not_allow_empty'
:
[{
'example'
:
'2'
}],
}
}
expected_data
=
{
expected_data
=
{
'allow_null'
:
None
,
'allow_null'
:
None
,
'not_allow_null'
:
[{
'example'
:
2
},
{
'example'
:
3
}]
'not_allow_null'
:
[{
'example'
:
2
},
{
'example'
:
3
}],
'allow_empty'
:
[{
'example'
:
2
}],
'not_allow_empty'
:
[{
'example'
:
2
}],
}
}
serializer
=
self
.
Serializer
(
data
=
input_data
)
serializer
=
self
.
Serializer
(
data
=
input_data
)
...
@@ -99,7 +105,9 @@ class TestNestedSerializerWithMany:
...
@@ -99,7 +105,9 @@ class TestNestedSerializerWithMany:
def
test_null_is_not_allowed_if_allow_null_is_not_set
(
self
):
def
test_null_is_not_allowed_if_allow_null_is_not_set
(
self
):
input_data
=
{
input_data
=
{
'allow_null'
:
None
,
'allow_null'
:
None
,
'not_allow_null'
:
None
'not_allow_null'
:
None
,
'allow_empty'
:
[{
'example'
:
'2'
}],
'not_allow_empty'
:
[{
'example'
:
'2'
}],
}
}
serializer
=
self
.
Serializer
(
data
=
input_data
)
serializer
=
self
.
Serializer
(
data
=
input_data
)
...
@@ -118,10 +126,44 @@ class TestNestedSerializerWithMany:
...
@@ -118,10 +126,44 @@ class TestNestedSerializerWithMany:
input_data
=
{
input_data
=
{
'allow_null'
:
None
,
'allow_null'
:
None
,
'not_allow_null'
:
[{
'example'
:
2
}]
'not_allow_null'
:
[{
'example'
:
2
}],
'allow_empty'
:
[{
'example'
:
2
}],
'not_allow_empty'
:
[{
'example'
:
2
}],
}
}
serializer
=
TestSerializer
(
data
=
input_data
)
serializer
=
TestSerializer
(
data
=
input_data
)
assert
serializer
.
is_valid
()
assert
serializer
.
is_valid
()
assert
serializer
.
validated_data
==
input_data
assert
serializer
.
validated_data
==
input_data
assert
TestSerializer
.
validation_was_run
assert
TestSerializer
.
validation_was_run
def
test_empty_allowed_if_allow_empty_is_set
(
self
):
input_data
=
{
'allow_null'
:
[{
'example'
:
'2'
}],
'not_allow_null'
:
[{
'example'
:
'2'
}],
'allow_empty'
:
[],
'not_allow_empty'
:
[{
'example'
:
'2'
}],
}
expected_data
=
{
'allow_null'
:
[{
'example'
:
2
}],
'not_allow_null'
:
[{
'example'
:
2
}],
'allow_empty'
:
[],
'not_allow_empty'
:
[{
'example'
:
2
}],
}
serializer
=
self
.
Serializer
(
data
=
input_data
)
assert
serializer
.
is_valid
(),
serializer
.
errors
assert
serializer
.
validated_data
==
expected_data
def
test_empty_not_allowed_if_allow_empty_is_set_to_false
(
self
):
input_data
=
{
'allow_null'
:
[{
'example'
:
'2'
}],
'not_allow_null'
:
[{
'example'
:
'2'
}],
'allow_empty'
:
[],
'not_allow_empty'
:
[],
}
serializer
=
self
.
Serializer
(
data
=
input_data
)
assert
not
serializer
.
is_valid
()
expected_errors
=
{
'not_allow_empty'
:
{
'non_field_errors'
:
[
serializers
.
ListSerializer
.
default_error_messages
[
'empty'
]]}}
assert
serializer
.
errors
==
expected_errors
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