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
a73c16b8
Commit
a73c16b8
authored
May 18, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
serializers.Field respects ordering on dicts if it exists. Closes #832
parent
b950b025
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
5 deletions
+22
-5
rest_framework/fields.py
+6
-1
rest_framework/tests/fields.py
+16
-4
No files found.
rest_framework/fields.py
View file @
a73c16b8
...
...
@@ -19,6 +19,7 @@ from django import forms
from
django.forms
import
widgets
from
django.utils.encoding
import
is_protected_type
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.utils.datastructures
import
SortedDict
from
rest_framework
import
ISO_8601
from
rest_framework.compat
import
timezone
,
parse_date
,
parse_datetime
,
parse_time
...
...
@@ -170,7 +171,11 @@ class Field(object):
elif
hasattr
(
value
,
'__iter__'
)
and
not
isinstance
(
value
,
(
dict
,
six
.
string_types
)):
return
[
self
.
to_native
(
item
)
for
item
in
value
]
elif
isinstance
(
value
,
dict
):
return
dict
(
map
(
self
.
to_native
,
(
k
,
v
))
for
k
,
v
in
value
.
items
())
# Make sure we preserve field ordering, if it exists
ret
=
SortedDict
()
for
key
,
val
in
value
.
items
():
ret
[
key
]
=
self
.
to_native
(
val
)
return
ret
return
smart_text
(
value
)
def
attributes
(
self
):
...
...
rest_framework/tests/fields.py
View file @
a73c16b8
...
...
@@ -2,13 +2,12 @@
General serializer field tests.
"""
from
__future__
import
unicode_literals
from
django.utils.datastructures
import
SortedDict
import
datetime
from
decimal
import
Decimal
from
django.db
import
models
from
django.test
import
TestCase
from
django.core
import
validators
from
rest_framework
import
serializers
from
rest_framework.serializers
import
Serializer
...
...
@@ -63,6 +62,20 @@ class BasicFieldTests(TestCase):
serializer
=
CharPrimaryKeyModelSerializer
()
self
.
assertEqual
(
serializer
.
fields
[
'id'
]
.
read_only
,
False
)
def
test_dict_field_ordering
(
self
):
"""
Field should preserve dictionary ordering, if it exists.
See: https://github.com/tomchristie/django-rest-framework/issues/832
"""
ret
=
SortedDict
()
ret
[
'c'
]
=
1
ret
[
'b'
]
=
1
ret
[
'a'
]
=
1
ret
[
'z'
]
=
1
field
=
serializers
.
Field
()
keys
=
list
(
field
.
to_native
(
ret
)
.
keys
())
self
.
assertEqual
(
keys
,
[
'c'
,
'b'
,
'a'
,
'z'
])
class
DateFieldTest
(
TestCase
):
"""
...
...
@@ -645,4 +658,4 @@ class DecimalFieldTest(TestCase):
s
=
DecimalSerializer
(
data
=
{
'decimal_field'
:
'12345.6'
})
self
.
assertFalse
(
s
.
is_valid
())
self
.
assertEqual
(
s
.
errors
,
{
'decimal_field'
:
[
'Ensure that there are no more than 4 digits in total.'
]})
\ No newline at end of file
self
.
assertEqual
(
s
.
errors
,
{
'decimal_field'
:
[
'Ensure that there are no more than 4 digits in total.'
]})
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