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
8c360770
Commit
8c360770
authored
Oct 25, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add pre_save hook in generic views
parent
3e751ccd
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
8 deletions
+10
-8
rest_framework/mixins.py
+10
-8
No files found.
rest_framework/mixins.py
View file @
8c360770
...
...
@@ -20,10 +20,14 @@ class CreateModelMixin(object):
def
create
(
self
,
request
,
*
args
,
**
kwargs
):
serializer
=
self
.
get_serializer
(
data
=
request
.
DATA
)
if
serializer
.
is_valid
():
self
.
pre_save
(
serializer
.
object
)
self
.
object
=
serializer
.
save
()
return
Response
(
serializer
.
data
,
status
=
status
.
HTTP_201_CREATED
)
return
Response
(
serializer
.
errors
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
def
pre_save
(
self
,
obj
):
pass
class
ListModelMixin
(
object
):
"""
...
...
@@ -46,7 +50,8 @@ class ListModelMixin(object):
# which may be `None` to disable pagination.
page_size
=
self
.
get_paginate_by
(
self
.
object_list
)
if
page_size
:
paginator
,
page
,
queryset
,
is_paginated
=
self
.
paginate_queryset
(
self
.
object_list
,
page_size
)
packed
=
self
.
paginate_queryset
(
self
.
object_list
,
page_size
)
paginator
,
page
,
queryset
,
is_paginated
=
packed
serializer
=
self
.
get_pagination_serializer
(
page
)
else
:
serializer
=
self
.
get_serializer
(
instance
=
self
.
object_list
)
...
...
@@ -79,20 +84,17 @@ class UpdateModelMixin(object):
serializer
=
self
.
get_serializer
(
data
=
request
.
DATA
,
instance
=
self
.
object
)
if
serializer
.
is_valid
():
if
self
.
object
is
None
:
# If PUT occurs to a non existant object, we need to set any
# attributes on the object that are implicit in the URL.
self
.
update_urlconf_attributes
(
serializer
.
object
)
self
.
pre_save
(
serializer
.
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
):
def
pre_save
(
self
,
obj
):
"""
When update (re)creates an object, we need to set any attributes that
are tied to the URLconf.
Set any attributes on the object that are implicit in the request.
"""
# pk and/or slug attributes are implicit in the URL.
pk
=
self
.
kwargs
.
get
(
self
.
pk_url_kwarg
,
None
)
if
pk
:
setattr
(
obj
,
'pk'
,
pk
)
...
...
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