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
24dec32e
Commit
24dec32e
authored
Aug 06, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added test_iter_options
parent
4d69286e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
1 deletions
+31
-1
rest_framework/fields.py
+6
-1
tests/test_fields.py
+25
-0
No files found.
rest_framework/fields.py
View file @
24dec32e
...
@@ -1192,20 +1192,25 @@ class ChoiceField(Field):
...
@@ -1192,20 +1192,25 @@ class ChoiceField(Field):
"""
"""
class
StartOptionGroup
(
object
):
class
StartOptionGroup
(
object
):
start_option_group
=
True
start_option_group
=
True
end_option_group
=
False
def
__init__
(
self
,
label
):
def
__init__
(
self
,
label
):
self
.
label
=
label
self
.
label
=
label
class
EndOptionGroup
(
object
):
class
EndOptionGroup
(
object
):
start_option_group
=
False
end_option_group
=
True
end_option_group
=
True
class
Option
(
object
):
class
Option
(
object
):
start_option_group
=
False
end_option_group
=
False
def
__init__
(
self
,
value
,
display_text
):
def
__init__
(
self
,
value
,
display_text
):
self
.
value
=
value
self
.
value
=
value
self
.
display_text
=
display_text
self
.
display_text
=
display_text
for
key
,
value
in
self
.
grouped_choices
.
items
():
for
key
,
value
in
self
.
grouped_choices
.
items
():
if
isinstance
(
value
,
(
list
,
tuple
)
):
if
isinstance
(
value
,
dict
):
yield
StartOptionGroup
(
label
=
key
)
yield
StartOptionGroup
(
label
=
key
)
for
sub_key
,
sub_value
in
value
.
items
():
for
sub_key
,
sub_value
in
value
.
items
():
yield
Option
(
value
=
sub_key
,
display_text
=
sub_value
)
yield
Option
(
value
=
sub_key
,
display_text
=
sub_value
)
...
...
tests/test_fields.py
View file @
24dec32e
...
@@ -1107,6 +1107,31 @@ class TestChoiceField(FieldValues):
...
@@ -1107,6 +1107,31 @@ class TestChoiceField(FieldValues):
output
=
field
.
run_validation
(
None
)
output
=
field
.
run_validation
(
None
)
assert
output
is
None
assert
output
is
None
def
test_iter_options
(
self
):
"""
iter_options() should return a list of options and option groups.
"""
field
=
serializers
.
ChoiceField
(
choices
=
[
(
'Numbers'
,
[
'integer'
,
'float'
]),
(
'Strings'
,
[
'text'
,
'email'
,
'url'
])
]
)
items
=
list
(
field
.
iter_options
())
assert
items
[
0
]
.
start_option_group
assert
items
[
0
]
.
label
==
'Numbers'
assert
items
[
1
]
.
value
==
'integer'
assert
items
[
2
]
.
value
==
'float'
assert
items
[
3
]
.
end_option_group
assert
items
[
4
]
.
start_option_group
assert
items
[
4
]
.
label
==
'Strings'
assert
items
[
5
]
.
value
==
'text'
assert
items
[
6
]
.
value
==
'email'
assert
items
[
7
]
.
value
==
'url'
assert
items
[
8
]
.
end_option_group
class
TestChoiceFieldWithType
(
FieldValues
):
class
TestChoiceFieldWithType
(
FieldValues
):
"""
"""
...
...
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