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
2533c245
Commit
2533c245
authored
Oct 05, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support PUT for create
parent
7218bcba
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
0 deletions
+40
-0
rest_framework/mixins.py
+25
-0
rest_framework/tests/generics.py
+15
-0
No files found.
rest_framework/mixins.py
View file @
2533c245
...
...
@@ -71,13 +71,38 @@ class UpdateModelMixin(object):
Should be mixed in with `SingleObjectBaseView`.
"""
def
update
(
self
,
request
,
*
args
,
**
kwargs
):
try
:
self
.
object
=
self
.
get_object
()
except
Http404
:
self
.
object
=
None
serializer
=
self
.
get_serializer
(
data
=
request
.
DATA
,
instance
=
self
.
object
)
if
serializer
.
is_valid
():
if
self
.
object
is
None
:
obj
=
serializer
.
object
# TODO: Make ModelSerializers return regular instances,
# not DeserializedObject
if
hasattr
(
obj
,
'object'
):
obj
=
obj
.
object
self
.
update_urlconf_attributes
(
serializer
.
object
.
object
)
self
.
object
=
serializer
.
save
()
return
Response
(
serializer
.
data
)
return
Response
(
serializer
.
errors
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
def
update_urlconf_attributes
(
self
,
obj
):
"""
When update (re)creates an object, we need to set any attributes that
are tied to the URLconf.
"""
pk
=
self
.
kwargs
.
get
(
self
.
pk_url_kwarg
,
None
)
if
pk
:
setattr
(
obj
,
'pk'
,
pk
)
slug
=
self
.
kwargs
.
get
(
self
.
slug_url_kwarg
,
None
)
if
slug
:
slug_field
=
self
.
get_slug_field
()
setattr
(
obj
,
slug_field
,
slug
)
class
DestroyModelMixin
(
object
):
"""
...
...
rest_framework/tests/generics.py
View file @
2533c245
...
...
@@ -208,3 +208,18 @@ class TestInstanceView(TestCase):
self
.
assertEquals
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
updated
=
self
.
objects
.
get
(
id
=
1
)
self
.
assertEquals
(
updated
.
text
,
'foobar'
)
def
test_put_to_deleted_instance
(
self
):
"""
PUT requests to RetrieveUpdateDestroyAPIView should create an object
if it does not currently exist.
"""
self
.
objects
.
get
(
id
=
1
)
.
delete
()
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
put
(
'/1'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
response
=
self
.
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'
)
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