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
e801e212
Commit
e801e212
authored
Nov 16, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #422 from markotibold/max_length_for_modelserializers
Max length for modelserializers
parents
19b0516b
0076e2f4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
0 deletions
+43
-0
docs/api-guide/fields.md
+12
-0
rest_framework/fields.py
+17
-0
rest_framework/serializers.py
+6
-0
rest_framework/tests/serializer.py
+8
-0
No files found.
docs/api-guide/fields.md
View file @
e801e212
...
@@ -131,6 +131,18 @@ or `django.db.models.fields.TextField`.
...
@@ -131,6 +131,18 @@ or `django.db.models.fields.TextField`.
**Signature:**
`CharField(max_length=None, min_length=None)`
**Signature:**
`CharField(max_length=None, min_length=None)`
## URLField
Corresponds to
`django.db.models.fields.URLField`
. Uses Django's
`django.core.validators.URLValidator`
for validation.
**Signature:**
`CharField(max_length=200, min_length=None)`
## SlugField
Corresponds to
`django.db.models.fields.SlugField`
.
**Signature:**
`CharField(max_length=50, min_length=None)`
## ChoiceField
## ChoiceField
A field that can accept a value out of a limited set of choices.
A field that can accept a value out of a limited set of choices.
...
...
rest_framework/fields.py
View file @
e801e212
...
@@ -706,6 +706,23 @@ class CharField(WritableField):
...
@@ -706,6 +706,23 @@ class CharField(WritableField):
return
smart_unicode
(
value
)
return
smart_unicode
(
value
)
class
URLField
(
CharField
):
type_name
=
'URLField'
def
__init__
(
self
,
**
kwargs
):
kwargs
[
'max_length'
]
=
kwargs
.
get
(
'max_length'
,
200
)
kwargs
[
'validators'
]
=
[
validators
.
URLValidator
()]
super
(
URLField
,
self
)
.
__init__
(
**
kwargs
)
class
SlugField
(
CharField
):
type_name
=
'SlugField'
def
__init__
(
self
,
*
args
,
**
kwargs
):
kwargs
[
'max_length'
]
=
kwargs
.
get
(
'max_length'
,
50
)
super
(
SlugField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
class
ChoiceField
(
WritableField
):
class
ChoiceField
(
WritableField
):
type_name
=
'ChoiceField'
type_name
=
'ChoiceField'
widget
=
widgets
.
Select
widget
=
widgets
.
Select
...
...
rest_framework/serializers.py
View file @
e801e212
...
@@ -429,6 +429,10 @@ class ModelSerializer(Serializer):
...
@@ -429,6 +429,10 @@ class ModelSerializer(Serializer):
kwargs
[
'choices'
]
=
model_field
.
flatchoices
kwargs
[
'choices'
]
=
model_field
.
flatchoices
return
ChoiceField
(
**
kwargs
)
return
ChoiceField
(
**
kwargs
)
max_length
=
getattr
(
model_field
,
'max_length'
,
None
)
if
max_length
:
kwargs
[
'max_length'
]
=
max_length
field_mapping
=
{
field_mapping
=
{
models
.
FloatField
:
FloatField
,
models
.
FloatField
:
FloatField
,
models
.
IntegerField
:
IntegerField
,
models
.
IntegerField
:
IntegerField
,
...
@@ -439,6 +443,8 @@ class ModelSerializer(Serializer):
...
@@ -439,6 +443,8 @@ class ModelSerializer(Serializer):
models
.
DateField
:
DateField
,
models
.
DateField
:
DateField
,
models
.
EmailField
:
EmailField
,
models
.
EmailField
:
EmailField
,
models
.
CharField
:
CharField
,
models
.
CharField
:
CharField
,
models
.
URLField
:
URLField
,
models
.
SlugField
:
SlugField
,
models
.
TextField
:
CharField
,
models
.
TextField
:
CharField
,
models
.
CommaSeparatedIntegerField
:
CharField
,
models
.
CommaSeparatedIntegerField
:
CharField
,
models
.
BooleanField
:
BooleanField
,
models
.
BooleanField
:
BooleanField
,
...
...
rest_framework/tests/serializer.py
View file @
e801e212
...
@@ -239,6 +239,14 @@ class ValidationTests(TestCase):
...
@@ -239,6 +239,14 @@ class ValidationTests(TestCase):
self
.
assertEquals
(
serializer
.
is_valid
(),
True
)
self
.
assertEquals
(
serializer
.
is_valid
(),
True
)
self
.
assertEquals
(
serializer
.
errors
,
{})
self
.
assertEquals
(
serializer
.
errors
,
{})
def
test_modelserializer_max_length_exceeded
(
self
):
data
=
{
'title'
:
'x'
*
201
,
}
serializer
=
ActionItemSerializer
(
data
=
data
)
self
.
assertEquals
(
serializer
.
is_valid
(),
False
)
self
.
assertEquals
(
serializer
.
errors
,
{
'title'
:
[
u'Ensure this value has at most 200 characters (it has 201).'
]})
class
MetadataTests
(
TestCase
):
class
MetadataTests
(
TestCase
):
def
test_empty
(
self
):
def
test_empty
(
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