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
a851294d
Commit
a851294d
authored
Jan 21, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get_ordering/get_queryset refactoring
parent
add6c88a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
28 deletions
+22
-28
djangorestframework/mixins.py
+22
-28
No files found.
djangorestframework/mixins.py
View file @
a851294d
...
@@ -455,10 +455,9 @@ class ResourceMixin(object):
...
@@ -455,10 +455,9 @@ class ResourceMixin(object):
else
:
else
:
return
None
return
None
##########
##########
class
InstanceMixin
(
object
):
class
InstanceMixin
(
object
):
"""
"""
`Mixin` class that is used to identify a `View` class as being the canonical identifier
`Mixin` class that is used to identify a `View` class as being the canonical identifier
...
@@ -495,6 +494,8 @@ class ModelMixin(object):
...
@@ -495,6 +494,8 @@ class ModelMixin(object):
handles the instance data creation/preaparation.
handles the instance data creation/preaparation.
"""
"""
queryset
=
None
def
build_query
(
self
,
*
args
,
**
kwargs
):
def
build_query
(
self
,
*
args
,
**
kwargs
):
""" Returns django.db.models.Q object to be used for the objects retrival.
""" Returns django.db.models.Q object to be used for the objects retrival.
...
@@ -564,6 +565,19 @@ class ModelMixin(object):
...
@@ -564,6 +565,19 @@ class ModelMixin(object):
model
=
self
.
resource
.
model
model
=
self
.
resource
.
model
return
model
.
objects
.
get
(
self
.
build_query
(
*
args
,
**
kwargs
))
return
model
.
objects
.
get
(
self
.
build_query
(
*
args
,
**
kwargs
))
def
get_queryset
(
self
):
"""
Return the queryset for this view.
"""
return
getattr
(
self
.
resource
,
'queryset'
,
self
.
resource
.
model
.
objects
.
all
())
def
get_ordering
(
self
):
"""
Return the ordering for this view.
"""
return
getattr
(
self
.
resource
,
'ordering'
,
None
)
class
ReadModelMixin
(
ModelMixin
):
class
ReadModelMixin
(
ModelMixin
):
"""
"""
...
@@ -634,7 +648,8 @@ class UpdateModelMixin(ModelMixin):
...
@@ -634,7 +648,8 @@ class UpdateModelMixin(ModelMixin):
def
put
(
self
,
request
,
*
args
,
**
kwargs
):
def
put
(
self
,
request
,
*
args
,
**
kwargs
):
model
=
self
.
resource
.
model
model
=
self
.
resource
.
model
# TODO: update on the url of a non-existing resource url doesn't work correctly at the moment - will end up with a new url
# TODO: update on the url of a non-existing resource url doesn't work
# correctly at the moment - will end up with a new url
try
:
try
:
self
.
model_instance
=
self
.
get_object
(
*
args
,
**
kwargs
)
self
.
model_instance
=
self
.
get_object
(
*
args
,
**
kwargs
)
...
@@ -667,36 +682,15 @@ class ListModelMixin(ModelMixin):
...
@@ -667,36 +682,15 @@ class ListModelMixin(ModelMixin):
Behavior to list a set of `model` instances on GET requests
Behavior to list a set of `model` instances on GET requests
"""
"""
# NB. Not obvious to me if it would be better to set this on the resource?
#
# Presumably it's more useful to have on the view, because that way you can
# have multiple views across different querysets mapping to the same resource.
#
# Perhaps it ought to be:
#
# 1) View.queryset
# 2) if None fall back to Resource.queryset
# 3) if None fall back to Resource.model.objects.all()
#
# Any feedback welcomed.
queryset
=
None
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
queryset
=
self
.
get_queryset
()
queryset
=
self
.
get_queryset
()
ordering
=
self
.
get_ordering
()
if
hasattr
(
self
,
'resource'
):
queryset
=
queryset
.
filter
(
self
.
build_query
(
**
kwargs
))
ordering
=
getattr
(
self
.
resource
,
'ordering'
,
None
)
else
:
ordering
=
None
if
ordering
:
if
ordering
:
args
=
as_tuple
(
ordering
)
queryset
=
queryset
.
order_by
(
*
ordering
)
queryset
=
queryset
.
order_by
(
*
args
)
return
queryset
.
filter
(
self
.
build_query
(
**
kwargs
))
def
get_queryset
(
self
):
return
queryset
model
=
self
.
resource
.
model
return
model
.
objects
.
all
()
if
self
.
queryset
is
None
else
self
.
queryset
########## Pagination Mixins ##########
########## Pagination Mixins ##########
...
...
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