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
e14391e0
Commit
e14391e0
authored
Jul 16, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix for ModelSerializer ChoiceField with nonstandard args. Closes #3126.
parent
713333d3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
0 deletions
+27
-0
rest_framework/serializers.py
+13
-0
tests/test_model_serializer.py
+14
-0
No files found.
rest_framework/serializers.py
View file @
e14391e0
...
...
@@ -1033,6 +1033,19 @@ class ModelSerializer(Serializer):
# Fields with choices get coerced into `ChoiceField`
# instead of using their regular typed field.
field_class
=
self
.
serializer_choice_field
# Some model fields may introduce kwargs that would not be valid
# for the choice field. We need to strip these out.
# Eg. models.DecimalField(max_digits=3, decimal_places=1, choices=DECIMAL_CHOICES)
valid_kwargs
=
set
((
'read_only'
,
'write_only'
,
'required'
,
'default'
,
'initial'
,
'source'
,
'label'
,
'help_text'
,
'style'
,
'error_messages'
,
'validators'
,
'allow_null'
,
'allow_blank'
,
'choices'
))
for
key
in
list
(
field_kwargs
.
keys
()):
if
key
not
in
valid_kwargs
:
field_kwargs
.
pop
(
key
)
if
not
issubclass
(
field_class
,
ModelField
):
# `model_field` is only valid for the fallback case of
...
...
tests/test_model_serializer.py
View file @
e14391e0
...
...
@@ -7,6 +7,8 @@ an appropriate set of serializer fields for each case.
"""
from
__future__
import
unicode_literals
import
decimal
import
django
import
pytest
from
django.core.exceptions
import
ImproperlyConfigured
...
...
@@ -70,6 +72,7 @@ class RegularFieldsModel(models.Model):
COLOR_CHOICES
=
((
'red'
,
'Red'
),
(
'blue'
,
'Blue'
),
(
'green'
,
'Green'
))
DECIMAL_CHOICES
=
((
'low'
,
decimal
.
Decimal
(
'0.1'
)),
(
'medium'
,
decimal
.
Decimal
(
'0.5'
)),
(
'high'
,
decimal
.
Decimal
(
'0.9'
)))
class
FieldOptionsModel
(
models
.
Model
):
...
...
@@ -82,6 +85,10 @@ class FieldOptionsModel(models.Model):
choices_field
=
models
.
CharField
(
max_length
=
100
,
choices
=
COLOR_CHOICES
)
class
MappingForChoicesWithNonStandardArgs
(
models
.
Model
):
choices_field_with_nonstandard_args
=
models
.
DecimalField
(
max_digits
=
3
,
decimal_places
=
1
,
choices
=
DECIMAL_CHOICES
)
class
TestModelSerializer
(
TestCase
):
def
test_create_method
(
self
):
class
TestSerializer
(
serializers
.
ModelSerializer
):
...
...
@@ -307,6 +314,13 @@ class TestRegularFieldMappings(TestCase):
ChildSerializer
()
.
fields
def
test_choices_with_nonstandard_args
(
self
):
class
ExampleSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
MappingForChoicesWithNonStandardArgs
ExampleSerializer
()
@pytest.mark.skipif
(
django
.
VERSION
<
(
1
,
8
),
reason
=
'DurationField is only available for django1.8+'
)
...
...
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