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
2e87de01
Commit
2e87de01
authored
Sep 26, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added ListField
parent
8b8623c5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
2 deletions
+61
-2
rest_framework/fields.py
+38
-0
rest_framework/serializers.py
+4
-2
tests/test_fields.py
+19
-0
No files found.
rest_framework/fields.py
View file @
2e87de01
...
...
@@ -881,6 +881,44 @@ class ImageField(Field):
# Advanced field types...
class
ListField
(
Field
):
child
=
None
initial
=
[]
default_error_messages
=
{
'not_a_list'
:
_
(
'Expected a list of items but got type `{input_type}`'
)
}
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
child
=
kwargs
.
pop
(
'child'
,
copy
.
deepcopy
(
self
.
child
))
assert
self
.
child
is
not
None
,
'`child` is a required argument.'
assert
not
inspect
.
isclass
(
self
.
child
),
'`child` has not been instantiated.'
super
(
ListField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
self
.
child
.
bind
(
field_name
=
''
,
parent
=
self
)
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
):
return
html
.
parse_html_list
(
dictionary
,
prefix
=
self
.
field_name
)
return
dictionary
.
get
(
self
.
field_name
,
empty
)
def
to_internal_value
(
self
,
data
):
"""
List of dicts of native values <- List of dicts of primitive datatypes.
"""
if
html
.
is_html_input
(
data
):
data
=
html
.
parse_html_list
(
data
)
if
isinstance
(
data
,
type
(
''
))
or
not
hasattr
(
data
,
'__iter__'
):
self
.
fail
(
'not_a_list'
,
input_type
=
type
(
data
)
.
__name__
)
return
[
self
.
child
.
run_validation
(
item
)
for
item
in
data
]
def
to_representation
(
self
,
data
):
"""
List of object instances -> List of dicts of primitive datatypes.
"""
return
[
self
.
child
.
to_representation
(
item
)
for
item
in
data
]
class
ReadOnlyField
(
Field
):
"""
A read-only field that simply returns the field value.
...
...
rest_framework/serializers.py
View file @
2e87de01
...
...
@@ -287,6 +287,9 @@ class Serializer(BaseSerializer):
return
representation
.
serializer_repr
(
self
,
indent
=
1
)
# There's some replication of `ListField` here,
# but that's probably better than obfuscating the call hierarchy.
class
ListSerializer
(
BaseSerializer
):
child
=
None
initial
=
[]
...
...
@@ -301,7 +304,7 @@ class ListSerializer(BaseSerializer):
def
get_value
(
self
,
dictionary
):
# We override the default field access in order to support
# lists in HTML forms.
if
is_html_input
(
dictionary
):
if
html
.
is_html_input
(
dictionary
):
return
html
.
parse_html_list
(
dictionary
,
prefix
=
self
.
field_name
)
return
dictionary
.
get
(
self
.
field_name
,
empty
)
...
...
@@ -311,7 +314,6 @@ class ListSerializer(BaseSerializer):
"""
if
html
.
is_html_input
(
data
):
data
=
html
.
parse_html_list
(
data
)
return
[
self
.
child
.
run_validation
(
item
)
for
item
in
data
]
def
to_representation
(
self
,
data
):
...
...
tests/test_fields.py
View file @
2e87de01
...
...
@@ -849,6 +849,25 @@ class TestMultipleChoiceField(FieldValues):
)
class
TestListField
(
FieldValues
):
"""
Values for `ListField`.
"""
valid_inputs
=
[
([
1
,
2
,
3
],
[
1
,
2
,
3
]),
([
'1'
,
'2'
,
'3'
],
[
1
,
2
,
3
])
]
invalid_inputs
=
[
(
'not a list'
,
[
'Expected a list of items but got type `str`'
]),
([
1
,
2
,
'error'
],
[
'A valid integer is required.'
])
]
outputs
=
[
([
1
,
2
,
3
],
[
1
,
2
,
3
]),
([
'1'
,
'2'
,
'3'
],
[
1
,
2
,
3
])
]
field
=
fields
.
ListField
(
child
=
fields
.
IntegerField
())
# Tests for SerializerMethodField.
# --------------------------------
...
...
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