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
d1b99f35
Commit
d1b99f35
authored
Oct 02, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added model form field -> serializer form field mapping
parent
f010a955
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
23 deletions
+27
-23
rest_framework/fields.py
+0
-16
rest_framework/serializers.py
+15
-1
rest_framework/tests/generics.py
+12
-6
No files found.
rest_framework/fields.py
View file @
d1b99f35
...
@@ -439,19 +439,3 @@ class FloatField(Field):
...
@@ -439,19 +439,3 @@ class FloatField(Field):
except
(
TypeError
,
ValueError
):
except
(
TypeError
,
ValueError
):
msg
=
self
.
error_messages
[
'invalid'
]
%
value
msg
=
self
.
error_messages
[
'invalid'
]
%
value
raise
ValidationError
(
msg
)
raise
ValidationError
(
msg
)
# field_mapping = {
# models.AutoField: IntegerField,
# models.BooleanField: BooleanField,
# models.CharField: CharField,
# models.DateTimeField: DateTimeField,
# models.DateField: DateField,
# models.BigIntegerField: IntegerField,
# models.IntegerField: IntegerField,
# models.PositiveIntegerField: IntegerField,
# models.FloatField: FloatField
# }
# def modelfield_to_serializerfield(field):
# return field_mapping.get(type(field), Field)
rest_framework/serializers.py
View file @
d1b99f35
...
@@ -3,6 +3,7 @@ import datetime
...
@@ -3,6 +3,7 @@ import datetime
import
types
import
types
from
decimal
import
Decimal
from
decimal
import
Decimal
from
django.core.serializers.base
import
DeserializedObject
from
django.core.serializers.base
import
DeserializedObject
from
django.db
import
models
from
django.utils.datastructures
import
SortedDict
from
django.utils.datastructures
import
SortedDict
from
rest_framework.compat
import
get_concrete_model
from
rest_framework.compat
import
get_concrete_model
from
rest_framework.fields
import
*
from
rest_framework.fields
import
*
...
@@ -349,7 +350,20 @@ class ModelSerializer(RelatedField, Serializer):
...
@@ -349,7 +350,20 @@ class ModelSerializer(RelatedField, Serializer):
"""
"""
Creates a default instance of a basic non-relational field.
Creates a default instance of a basic non-relational field.
"""
"""
return
Field
()
field_mapping
=
dict
([
[
models
.
FloatField
,
FloatField
],
[
models
.
IntegerField
,
IntegerField
],
[
models
.
DateTimeField
,
DateTimeField
],
[
models
.
DateField
,
DateField
],
[
models
.
EmailField
,
EmailField
],
[
models
.
CharField
,
CharField
],
[
models
.
CommaSeparatedIntegerField
,
CharField
],
[
models
.
BooleanField
,
BooleanField
]
])
try
:
return
field_mapping
[
model_field
.
__class__
]()
except
KeyError
:
return
Field
()
def
restore_object
(
self
,
attrs
,
instance
=
None
):
def
restore_object
(
self
,
attrs
,
instance
=
None
):
"""
"""
...
...
rest_framework/tests/generics.py
View file @
d1b99f35
...
@@ -52,7 +52,8 @@ class TestRootView(TestCase):
...
@@ -52,7 +52,8 @@ class TestRootView(TestCase):
POST requests to RootAPIView should create a new object.
POST requests to RootAPIView should create a new object.
"""
"""
content
=
{
'text'
:
'foobar'
}
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
post
(
'/'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
request
=
factory
.
post
(
'/'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
response
=
self
.
view
(
request
)
.
render
()
response
=
self
.
view
(
request
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEquals
(
response
.
data
,
{
'id'
:
4
,
'text'
:
u'foobar'
})
self
.
assertEquals
(
response
.
data
,
{
'id'
:
4
,
'text'
:
u'foobar'
})
...
@@ -64,7 +65,8 @@ class TestRootView(TestCase):
...
@@ -64,7 +65,8 @@ class TestRootView(TestCase):
PUT requests to RootAPIView should not be allowed
PUT requests to RootAPIView should not be allowed
"""
"""
content
=
{
'text'
:
'foobar'
}
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
put
(
'/'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
request
=
factory
.
put
(
'/'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
response
=
self
.
view
(
request
)
.
render
()
response
=
self
.
view
(
request
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_405_METHOD_NOT_ALLOWED
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_405_METHOD_NOT_ALLOWED
)
self
.
assertEquals
(
response
.
data
,
{
"detail"
:
"Method 'PUT' not allowed."
})
self
.
assertEquals
(
response
.
data
,
{
"detail"
:
"Method 'PUT' not allowed."
})
...
@@ -105,7 +107,8 @@ class TestRootView(TestCase):
...
@@ -105,7 +107,8 @@ class TestRootView(TestCase):
POST requests to create a new object should not be able to set the id.
POST requests to create a new object should not be able to set the id.
"""
"""
content
=
{
'id'
:
999
,
'text'
:
'foobar'
}
content
=
{
'id'
:
999
,
'text'
:
'foobar'
}
request
=
factory
.
post
(
'/'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
request
=
factory
.
post
(
'/'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
response
=
self
.
view
(
request
)
.
render
()
response
=
self
.
view
(
request
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEquals
(
response
.
data
,
{
'id'
:
4
,
'text'
:
u'foobar'
})
self
.
assertEquals
(
response
.
data
,
{
'id'
:
4
,
'text'
:
u'foobar'
})
...
@@ -142,7 +145,8 @@ class TestInstanceView(TestCase):
...
@@ -142,7 +145,8 @@ class TestInstanceView(TestCase):
POST requests to InstanceAPIView should not be allowed
POST requests to InstanceAPIView should not be allowed
"""
"""
content
=
{
'text'
:
'foobar'
}
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
post
(
'/'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
request
=
factory
.
post
(
'/'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
response
=
self
.
view
(
request
)
.
render
()
response
=
self
.
view
(
request
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_405_METHOD_NOT_ALLOWED
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_405_METHOD_NOT_ALLOWED
)
self
.
assertEquals
(
response
.
data
,
{
"detail"
:
"Method 'POST' not allowed."
})
self
.
assertEquals
(
response
.
data
,
{
"detail"
:
"Method 'POST' not allowed."
})
...
@@ -152,7 +156,8 @@ class TestInstanceView(TestCase):
...
@@ -152,7 +156,8 @@ class TestInstanceView(TestCase):
PUT requests to InstanceAPIView should update an object.
PUT requests to InstanceAPIView should update an object.
"""
"""
content
=
{
'text'
:
'foobar'
}
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
put
(
'/1'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
request
=
factory
.
put
(
'/1'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEquals
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
self
.
assertEquals
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
...
@@ -197,7 +202,8 @@ class TestInstanceView(TestCase):
...
@@ -197,7 +202,8 @@ class TestInstanceView(TestCase):
POST requests to create a new object should not be able to set the id.
POST requests to create a new object should not be able to set the id.
"""
"""
content
=
{
'id'
:
999
,
'text'
:
'foobar'
}
content
=
{
'id'
:
999
,
'text'
:
'foobar'
}
request
=
factory
.
put
(
'/1'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
request
=
factory
.
put
(
'/1'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEquals
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
self
.
assertEquals
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
...
...
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