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
0c81d041
Commit
0c81d041
authored
May 18, 2013
by
Stephan Groß
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add min_value=0 to autogenerated Pos..IntFields
parent
3f47eb7a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
0 deletions
+77
-0
rest_framework/serializers.py
+4
-0
rest_framework/tests/serializer.py
+73
-0
No files found.
rest_framework/serializers.py
View file @
0c81d041
...
...
@@ -739,6 +739,10 @@ class ModelSerializer(Serializer):
if
issubclass
(
model_field
.
__class__
,
models
.
TextField
):
kwargs
[
'widget'
]
=
widgets
.
Textarea
if
issubclass
(
model_field
.
__class__
,
models
.
PositiveIntegerField
)
or
\
issubclass
(
model_field
.
__class__
,
models
.
PositiveSmallIntegerField
):
kwargs
[
'min_value'
]
=
0
# TODO: TypedChoiceField?
if
model_field
.
flatchoices
:
# This ModelField contains choices
kwargs
[
'choices'
]
=
model_field
.
flatchoices
...
...
rest_framework/tests/serializer.py
View file @
0c81d041
...
...
@@ -1402,3 +1402,76 @@ class AttributeMappingOnAutogeneratedFieldsTests(TestCase):
def
test_url_field
(
self
):
self
.
field_test
(
'url_field'
)
class
DefaultValuesOnAutogeneratedFieldsTests
(
TestCase
):
def
setUp
(
self
):
class
DVOAFModel
(
RESTFrameworkModel
):
positive_integer_field
=
models
.
PositiveIntegerField
(
blank
=
True
)
positive_small_integer_field
=
models
.
PositiveSmallIntegerField
(
blank
=
True
)
email_field
=
models
.
EmailField
(
blank
=
True
)
file_field
=
models
.
FileField
(
blank
=
True
)
image_field
=
models
.
ImageField
(
blank
=
True
)
slug_field
=
models
.
SlugField
(
blank
=
True
)
url_field
=
models
.
URLField
(
blank
=
True
)
class
DVOAFSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
DVOAFModel
self
.
serializer_class
=
DVOAFSerializer
self
.
fields_attributes
=
{
'positive_integer_field'
:
[
(
'min_value'
,
0
),
],
'positive_small_integer_field'
:
[
(
'min_value'
,
0
),
],
'email_field'
:
[
(
'max_length'
,
75
),
],
'file_field'
:
[
(
'max_length'
,
100
),
],
'image_field'
:
[
(
'max_length'
,
100
),
],
'slug_field'
:
[
(
'max_length'
,
50
),
],
'url_field'
:
[
(
'max_length'
,
200
),
],
}
def
field_test
(
self
,
field
):
serializer
=
self
.
serializer_class
(
data
=
{})
self
.
assertEqual
(
serializer
.
is_valid
(),
True
)
for
attribute
in
self
.
fields_attributes
[
field
]:
self
.
assertEqual
(
getattr
(
serializer
.
fields
[
field
],
attribute
[
0
]),
attribute
[
1
]
)
def
test_positive_integer_field
(
self
):
self
.
field_test
(
'positive_integer_field'
)
def
test_positive_small_integer_field
(
self
):
self
.
field_test
(
'positive_small_integer_field'
)
def
test_email_field
(
self
):
self
.
field_test
(
'email_field'
)
def
test_file_field
(
self
):
self
.
field_test
(
'file_field'
)
def
test_image_field
(
self
):
self
.
field_test
(
'image_field'
)
def
test_slug_field
(
self
):
self
.
field_test
(
'slug_field'
)
def
test_url_field
(
self
):
self
.
field_test
(
'url_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