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
1420c764
Commit
1420c764
authored
Sep 25, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ensure proper sorting of 'choices' attribute on ChoiceField
parent
fb1546ee
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
10 additions
and
7 deletions
+10
-7
rest_framework/fields.py
+6
-3
tests/test_fields.py
+2
-2
tests/test_metadata.py
+2
-2
No files found.
rest_framework/fields.py
View file @
1420c764
...
@@ -2,6 +2,7 @@ from django.conf import settings
...
@@ -2,6 +2,7 @@ from django.conf import settings
from
django.core
import
validators
from
django.core
import
validators
from
django.core.exceptions
import
ValidationError
from
django.core.exceptions
import
ValidationError
from
django.utils
import
timezone
from
django.utils
import
timezone
from
django.utils.datastructures
import
SortedDict
from
django.utils.dateparse
import
parse_date
,
parse_datetime
,
parse_time
from
django.utils.dateparse
import
parse_date
,
parse_datetime
,
parse_time
from
django.utils.encoding
import
is_protected_type
from
django.utils.encoding
import
is_protected_type
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.utils.translation
import
ugettext_lazy
as
_
...
@@ -166,7 +167,7 @@ class Field(object):
...
@@ -166,7 +167,7 @@ class Field(object):
# my_field = serializer.CharField(source='my_field')
# my_field = serializer.CharField(source='my_field')
assert
self
.
_kwargs
.
get
(
'source'
)
!=
field_name
,
(
assert
self
.
_kwargs
.
get
(
'source'
)
!=
field_name
,
(
"It is redundant to specify `source='
%
s'` on field '
%
s' in "
"It is redundant to specify `source='
%
s'` on field '
%
s' in "
"serializer '
%
s',
as it is the same
the field name. "
"serializer '
%
s',
because it is the same as
the field name. "
"Remove the `source` keyword argument."
%
"Remove the `source` keyword argument."
%
(
field_name
,
self
.
__class__
.
__name__
,
parent
.
__class__
.
__name__
)
(
field_name
,
self
.
__class__
.
__name__
,
parent
.
__class__
.
__name__
)
)
)
...
@@ -303,6 +304,7 @@ class BooleanField(Field):
...
@@ -303,6 +304,7 @@ class BooleanField(Field):
'invalid'
:
_
(
'`{input}` is not a valid boolean.'
)
'invalid'
:
_
(
'`{input}` is not a valid boolean.'
)
}
}
default_empty_html
=
False
default_empty_html
=
False
initial
=
False
TRUE_VALUES
=
set
((
't'
,
'T'
,
'true'
,
'True'
,
'TRUE'
,
'1'
,
1
,
True
))
TRUE_VALUES
=
set
((
't'
,
'T'
,
'true'
,
'True'
,
'TRUE'
,
'1'
,
1
,
True
))
FALSE_VALUES
=
set
((
'f'
,
'F'
,
'false'
,
'False'
,
'FALSE'
,
'0'
,
0
,
0.0
,
False
))
FALSE_VALUES
=
set
((
'f'
,
'F'
,
'false'
,
'False'
,
'FALSE'
,
'0'
,
0
,
0.0
,
False
))
...
@@ -365,6 +367,7 @@ class CharField(Field):
...
@@ -365,6 +367,7 @@ class CharField(Field):
'blank'
:
_
(
'This field may not be blank.'
)
'blank'
:
_
(
'This field may not be blank.'
)
}
}
default_empty_html
=
''
default_empty_html
=
''
initial
=
''
def
__init__
(
self
,
**
kwargs
):
def
__init__
(
self
,
**
kwargs
):
self
.
allow_blank
=
kwargs
.
pop
(
'allow_blank'
,
False
)
self
.
allow_blank
=
kwargs
.
pop
(
'allow_blank'
,
False
)
...
@@ -775,9 +778,9 @@ class ChoiceField(Field):
...
@@ -775,9 +778,9 @@ class ChoiceField(Field):
for
item
in
choices
for
item
in
choices
]
]
if
all
(
pairs
):
if
all
(
pairs
):
self
.
choices
=
d
ict
([(
key
,
display_value
)
for
key
,
display_value
in
choices
])
self
.
choices
=
SortedD
ict
([(
key
,
display_value
)
for
key
,
display_value
in
choices
])
else
:
else
:
self
.
choices
=
d
ict
([(
item
,
item
)
for
item
in
choices
])
self
.
choices
=
SortedD
ict
([(
item
,
item
)
for
item
in
choices
])
# Map the string representation of choices to the underlying value.
# Map the string representation of choices to the underlying value.
# Allows us to deal with eg. integer choices while supporting either
# Allows us to deal with eg. integer choices while supporting either
...
...
tests/test_fields.py
View file @
1420c764
...
@@ -76,8 +76,8 @@ class TestFieldOptions:
...
@@ -76,8 +76,8 @@ class TestFieldOptions:
ExampleSerializer
()
ExampleSerializer
()
assert
str
(
exc_info
.
value
)
==
(
assert
str
(
exc_info
.
value
)
==
(
"It is redundant to specify `source='example_field'` on field "
"It is redundant to specify `source='example_field'` on field "
"'CharField' in serializer 'ExampleSerializer',
as
it is the "
"'CharField' in serializer 'ExampleSerializer',
because
it is the "
"same the field name. Remove the `source` keyword argument."
"same
as
the field name. Remove the `source` keyword argument."
)
)
...
...
tests/test_metadata.py
View file @
1420c764
...
@@ -84,9 +84,9 @@ class TestMetadata:
...
@@ -84,9 +84,9 @@ class TestMetadata:
'read_only'
:
False
,
'read_only'
:
False
,
'label'
:
'Choice field'
,
'label'
:
'Choice field'
,
'choices'
:
[
'choices'
:
[
{
'display_name'
:
'
blue'
,
'value'
:
'blue
'
},
{
'display_name'
:
'
red'
,
'value'
:
'red
'
},
{
'display_name'
:
'green'
,
'value'
:
'green'
},
{
'display_name'
:
'green'
,
'value'
:
'green'
},
{
'display_name'
:
'
red'
,
'value'
:
'red
'
}
{
'display_name'
:
'
blue'
,
'value'
:
'blue
'
}
]
]
},
},
'integer_field'
:
{
'integer_field'
:
{
...
...
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