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
9f71f8e6
Commit
9f71f8e6
authored
Sep 28, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix broken bits of generic views
parent
4ebd701b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
6 deletions
+84
-6
rest_framework/serializers.py
+9
-5
rest_framework/tests/generics.py
+75
-1
No files found.
rest_framework/serializers.py
View file @
9f71f8e6
...
...
@@ -231,6 +231,9 @@ class BaseSerializer(Field):
"""
Serialize objects -> primatives.
"""
if
isinstance
(
obj
,
DeserializedObject
):
obj
=
obj
.
object
if
isinstance
(
obj
,
dict
):
return
dict
([(
key
,
self
.
to_native
(
val
))
for
(
key
,
val
)
in
obj
.
items
()])
...
...
@@ -295,11 +298,7 @@ class ModelSerializer(RelatedField, Serializer):
"""
Return all the fields that should be serialized for the model.
"""
if
serialize
:
cls
=
obj
.
__class__
else
:
cls
=
self
.
opts
.
model
cls
=
self
.
opts
.
model
opts
=
get_concrete_model
(
cls
)
.
_meta
pk_field
=
opts
.
pk
while
pk_field
.
rel
:
...
...
@@ -342,6 +341,11 @@ class ModelSerializer(RelatedField, Serializer):
"""
Restore the model instance.
"""
if
instance
:
for
key
,
val
in
attrs
.
items
():
setattr
(
instance
,
key
,
val
)
return
DeserializedObject
(
instance
)
m2m_data
=
{}
for
field
in
self
.
opts
.
model
.
_meta
.
many_to_many
:
if
field
.
name
in
attrs
:
...
...
rest_framework/tests/generics.py
View file @
9f71f8e6
from
django.test
import
TestCase
from
django.test.client
import
RequestFactory
from
django.utils
import
simplejson
as
json
from
rest_framework
import
generics
,
status
from
rest_framework.tests.models
import
BasicModel
...
...
@@ -11,8 +12,15 @@ class RootView(generics.RootAPIView):
model
=
BasicModel
class
TestListView
(
TestCase
):
class
InstanceView
(
generics
.
InstanceAPIView
):
model
=
BasicModel
class
TestRootView
(
TestCase
):
def
setUp
(
self
):
"""
Create 3 BasicModel intances.
"""
items
=
[
'foo'
,
'bar'
,
'baz'
]
for
item
in
items
:
BasicModel
(
text
=
item
)
.
save
()
...
...
@@ -23,8 +31,74 @@ class TestListView(TestCase):
]
def
test_get_root_view
(
self
):
"""
GET requests to RootAPIView should return list of objects.
"""
view
=
RootView
.
as_view
()
request
=
factory
.
get
(
'/'
)
response
=
view
(
request
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEquals
(
response
.
data
,
self
.
data
)
def
test_post_root_view
(
self
):
"""
POST requests to RootAPIView should create a new object.
"""
view
=
RootView
.
as_view
()
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
post
(
'/'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
response
=
view
(
request
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEquals
(
response
.
data
,
{
'id'
:
4
,
'text'
:
u'foobar'
})
created
=
self
.
objects
.
get
(
id
=
4
)
self
.
assertEquals
(
created
.
text
,
'foobar'
)
class
TestInstanceView
(
TestCase
):
def
setUp
(
self
):
"""
Create 3 BasicModel intances.
"""
items
=
[
'foo'
,
'bar'
,
'baz'
]
for
item
in
items
:
BasicModel
(
text
=
item
)
.
save
()
self
.
objects
=
BasicModel
.
objects
self
.
data
=
[
{
'id'
:
obj
.
id
,
'text'
:
obj
.
text
}
for
obj
in
self
.
objects
.
all
()
]
def
test_get_instance_view
(
self
):
"""
GET requests to InstanceAPIView should return a single object.
"""
view
=
InstanceView
.
as_view
()
request
=
factory
.
get
(
'/1'
)
response
=
view
(
request
,
pk
=
1
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEquals
(
response
.
data
,
self
.
data
[
0
])
def
test_put_instance_view
(
self
):
"""
PUT requests to InstanceAPIView should update an object.
"""
view
=
InstanceView
.
as_view
()
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
put
(
'/1'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
response
=
view
(
request
,
pk
=
1
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEquals
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
updated
=
self
.
objects
.
get
(
id
=
1
)
self
.
assertEquals
(
updated
.
text
,
'foobar'
)
def
test_delete_instance_view
(
self
):
"""
DELETE requests to InstanceAPIView should delete an object.
"""
view
=
InstanceView
.
as_view
()
request
=
factory
.
delete
(
'/1'
)
response
=
view
(
request
,
pk
=
1
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_204_NO_CONTENT
)
self
.
assertEquals
(
response
.
content
,
''
)
ids
=
[
obj
.
id
for
obj
in
self
.
objects
.
all
()]
self
.
assertEquals
(
ids
,
[
2
,
3
])
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