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
6c108c45
Commit
6c108c45
authored
Apr 16, 2014
by
Ian Foote
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow customising ChoiceField blank display value
parent
1d404874
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
2 deletions
+15
-2
rest_framework/fields.py
+6
-2
rest_framework/tests/test_fields.py
+9
-0
No files found.
rest_framework/fields.py
View file @
6c108c45
...
...
@@ -509,12 +509,16 @@ class ChoiceField(WritableField):
'the available choices.'
),
}
def
__init__
(
self
,
choices
=
(),
*
args
,
**
kwargs
):
def
__init__
(
self
,
choices
=
(),
blank_display_value
=
None
,
*
args
,
**
kwargs
):
self
.
empty
=
kwargs
.
pop
(
'empty'
,
''
)
super
(
ChoiceField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
self
.
choices
=
choices
if
not
self
.
required
:
self
.
choices
=
BLANK_CHOICE_DASH
+
self
.
choices
if
blank_display_value
is
None
:
blank_choice
=
BLANK_CHOICE_DASH
else
:
blank_choice
=
[(
''
,
blank_display_value
)]
self
.
choices
=
blank_choice
+
self
.
choices
def
_get_choices
(
self
):
return
self
.
_choices
...
...
rest_framework/tests/test_fields.py
View file @
6c108c45
...
...
@@ -706,6 +706,15 @@ class ChoiceFieldTests(TestCase):
f
=
serializers
.
ChoiceField
(
required
=
False
,
choices
=
SAMPLE_CHOICES
)
self
.
assertEqual
(
f
.
choices
,
models
.
fields
.
BLANK_CHOICE_DASH
+
SAMPLE_CHOICES
)
def
test_blank_choice_display
(
self
):
blank
=
'No Preference'
f
=
serializers
.
ChoiceField
(
required
=
False
,
choices
=
SAMPLE_CHOICES
,
blank_display_value
=
blank
,
)
self
.
assertEqual
(
f
.
choices
,
[(
''
,
blank
)]
+
SAMPLE_CHOICES
)
def
test_invalid_choice_model
(
self
):
s
=
ChoiceFieldModelSerializer
(
data
=
{
'choice'
:
'wrong_value'
})
self
.
assertFalse
(
s
.
is_valid
())
...
...
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