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
0c95405e
Commit
0c95405e
authored
May 18, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #853 from pyriku/725-blank-choice-dash
Adds BLANK_CHOICE_DASH as a choice item
parents
2a3056d0
8fe43236
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
100 additions
and
3 deletions
+100
-3
rest_framework/fields.py
+3
-0
rest_framework/serializers.py
+2
-3
rest_framework/tests/fields.py
+26
-0
rest_framework/tests/serializer.py
+69
-0
No files found.
rest_framework/fields.py
View file @
0c95405e
...
@@ -15,6 +15,7 @@ import warnings
...
@@ -15,6 +15,7 @@ import warnings
from
django.core
import
validators
from
django.core
import
validators
from
django.core.exceptions
import
ValidationError
from
django.core.exceptions
import
ValidationError
from
django.conf
import
settings
from
django.conf
import
settings
from
django.db.models.fields
import
BLANK_CHOICE_DASH
from
django
import
forms
from
django
import
forms
from
django.forms
import
widgets
from
django.forms
import
widgets
from
django.utils.encoding
import
is_protected_type
from
django.utils.encoding
import
is_protected_type
...
@@ -407,6 +408,8 @@ class ChoiceField(WritableField):
...
@@ -407,6 +408,8 @@ class ChoiceField(WritableField):
def
__init__
(
self
,
choices
=
(),
*
args
,
**
kwargs
):
def
__init__
(
self
,
choices
=
(),
*
args
,
**
kwargs
):
super
(
ChoiceField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
super
(
ChoiceField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
self
.
choices
=
choices
self
.
choices
=
choices
if
not
self
.
required
:
self
.
choices
=
BLANK_CHOICE_DASH
+
self
.
choices
def
_get_choices
(
self
):
def
_get_choices
(
self
):
return
self
.
_choices
return
self
.
_choices
...
...
rest_framework/serializers.py
View file @
0c95405e
...
@@ -705,15 +705,14 @@ class ModelSerializer(Serializer):
...
@@ -705,15 +705,14 @@ class ModelSerializer(Serializer):
Creates a default instance of a basic non-relational field.
Creates a default instance of a basic non-relational field.
"""
"""
kwargs
=
{}
kwargs
=
{}
has_default
=
model_field
.
has_default
()
if
model_field
.
null
or
model_field
.
blank
or
has_default
:
if
model_field
.
null
or
model_field
.
blank
:
kwargs
[
'required'
]
=
False
kwargs
[
'required'
]
=
False
if
isinstance
(
model_field
,
models
.
AutoField
)
or
not
model_field
.
editable
:
if
isinstance
(
model_field
,
models
.
AutoField
)
or
not
model_field
.
editable
:
kwargs
[
'read_only'
]
=
True
kwargs
[
'read_only'
]
=
True
if
has_default
:
if
model_field
.
has_default
()
:
kwargs
[
'default'
]
=
model_field
.
get_default
()
kwargs
[
'default'
]
=
model_field
.
get_default
()
if
issubclass
(
model_field
.
__class__
,
models
.
TextField
):
if
issubclass
(
model_field
.
__class__
,
models
.
TextField
):
...
...
rest_framework/tests/fields.py
View file @
0c95405e
...
@@ -659,3 +659,29 @@ class DecimalFieldTest(TestCase):
...
@@ -659,3 +659,29 @@ class DecimalFieldTest(TestCase):
self
.
assertFalse
(
s
.
is_valid
())
self
.
assertFalse
(
s
.
is_valid
())
self
.
assertEqual
(
s
.
errors
,
{
'decimal_field'
:
[
'Ensure that there are no more than 4 digits in total.'
]})
self
.
assertEqual
(
s
.
errors
,
{
'decimal_field'
:
[
'Ensure that there are no more than 4 digits in total.'
]})
class
ChoiceFieldTests
(
TestCase
):
"""
Tests for the ChoiceField options generator
"""
SAMPLE_CHOICES
=
[
(
'red'
,
'Red'
),
(
'green'
,
'Green'
),
(
'blue'
,
'Blue'
),
]
def
test_choices_required
(
self
):
"""
Make sure proper choices are rendered if field is required
"""
f
=
serializers
.
ChoiceField
(
required
=
True
,
choices
=
self
.
SAMPLE_CHOICES
)
self
.
assertEqual
(
f
.
choices
,
self
.
SAMPLE_CHOICES
)
def
test_choices_not_required
(
self
):
"""
Make sure proper choices (plus blank) are rendered if the field isn't required
"""
f
=
serializers
.
ChoiceField
(
required
=
False
,
choices
=
self
.
SAMPLE_CHOICES
)
self
.
assertEqual
(
f
.
choices
,
models
.
fields
.
BLANK_CHOICE_DASH
+
self
.
SAMPLE_CHOICES
)
rest_framework/tests/serializer.py
View file @
0c95405e
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
from
django.db
import
models
from
django.db.models.fields
import
BLANK_CHOICE_DASH
from
django.utils.datastructures
import
MultiValueDict
from
django.utils.datastructures
import
MultiValueDict
from
django.test
import
TestCase
from
django.test
import
TestCase
from
rest_framework
import
serializers
from
rest_framework
import
serializers
...
@@ -1001,6 +1003,73 @@ class SerializerPickleTests(TestCase):
...
@@ -1001,6 +1003,73 @@ class SerializerPickleTests(TestCase):
repr
(
pickle
.
loads
(
pickle
.
dumps
(
data
,
0
)))
repr
(
pickle
.
loads
(
pickle
.
dumps
(
data
,
0
)))
# test for issue #725
class
SeveralChoicesModel
(
models
.
Model
):
color
=
models
.
CharField
(
max_length
=
10
,
choices
=
[(
'red'
,
'Red'
),
(
'green'
,
'Green'
),
(
'blue'
,
'Blue'
)],
blank
=
False
)
drink
=
models
.
CharField
(
max_length
=
10
,
choices
=
[(
'beer'
,
'Beer'
),
(
'wine'
,
'Wine'
),
(
'cider'
,
'Cider'
)],
blank
=
False
,
default
=
'beer'
)
os
=
models
.
CharField
(
max_length
=
10
,
choices
=
[(
'linux'
,
'Linux'
),
(
'osx'
,
'OSX'
),
(
'windows'
,
'Windows'
)],
blank
=
True
)
music_genre
=
models
.
CharField
(
max_length
=
10
,
choices
=
[(
'rock'
,
'Rock'
),
(
'metal'
,
'Metal'
),
(
'grunge'
,
'Grunge'
)],
blank
=
True
,
default
=
'metal'
)
class
SerializerChoiceFields
(
TestCase
):
def
setUp
(
self
):
super
(
SerializerChoiceFields
,
self
)
.
setUp
()
class
SeveralChoicesSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
SeveralChoicesModel
fields
=
(
'color'
,
'drink'
,
'os'
,
'music_genre'
)
self
.
several_choices_serializer
=
SeveralChoicesSerializer
def
test_choices_blank_false_not_default
(
self
):
serializer
=
self
.
several_choices_serializer
()
self
.
assertEqual
(
serializer
.
fields
[
'color'
]
.
choices
,
[(
'red'
,
'Red'
),
(
'green'
,
'Green'
),
(
'blue'
,
'Blue'
)]
)
def
test_choices_blank_false_with_default
(
self
):
serializer
=
self
.
several_choices_serializer
()
self
.
assertEqual
(
serializer
.
fields
[
'drink'
]
.
choices
,
[(
'beer'
,
'Beer'
),
(
'wine'
,
'Wine'
),
(
'cider'
,
'Cider'
)]
)
def
test_choices_blank_true_not_default
(
self
):
serializer
=
self
.
several_choices_serializer
()
self
.
assertEqual
(
serializer
.
fields
[
'os'
]
.
choices
,
BLANK_CHOICE_DASH
+
[(
'linux'
,
'Linux'
),
(
'osx'
,
'OSX'
),
(
'windows'
,
'Windows'
)]
)
def
test_choices_blank_true_with_default
(
self
):
serializer
=
self
.
several_choices_serializer
()
self
.
assertEqual
(
serializer
.
fields
[
'music_genre'
]
.
choices
,
BLANK_CHOICE_DASH
+
[(
'rock'
,
'Rock'
),
(
'metal'
,
'Metal'
),
(
'grunge'
,
'Grunge'
)]
)
class
DepthTest
(
TestCase
):
class
DepthTest
(
TestCase
):
def
test_implicit_nesting
(
self
):
def
test_implicit_nesting
(
self
):
...
...
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