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
894f6325
Commit
894f6325
authored
Feb 01, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove use of Q objects.
parent
7886fa2b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
39 deletions
+20
-39
djangorestframework/mixins.py
+20
-39
No files found.
djangorestframework/mixins.py
View file @
894f6325
...
@@ -6,7 +6,6 @@ classes that can be added to a `View`.
...
@@ -6,7 +6,6 @@ classes that can be added to a `View`.
from
django.contrib.auth.models
import
AnonymousUser
from
django.contrib.auth.models
import
AnonymousUser
from
django.core.paginator
import
Paginator
from
django.core.paginator
import
Paginator
from
django.db.models.fields.related
import
ForeignKey
from
django.db.models.fields.related
import
ForeignKey
from
django.db.models.query
import
Q
from
django.http
import
HttpResponse
from
django.http
import
HttpResponse
from
urlobject
import
URLObject
from
urlobject
import
URLObject
...
@@ -479,39 +478,25 @@ class ModelMixin(object):
...
@@ -479,39 +478,25 @@ class ModelMixin(object):
queryset
=
None
queryset
=
None
def
build_query
(
self
,
*
args
,
**
kwargs
):
def
get_query_kwargs
(
self
,
*
args
,
**
kwargs
):
""" Returns django.db.models.Q object to be used for the objects retrival.
"""
Return a dict of kwargs that will be used to build the
Arguments:
model instance retrieval or to filter querysets.
- args: unnamed URL arguments
- kwargs: named URL arguments
If a URL passes any arguments to the view being the QueryMixin subclass
build_query manages the arguments and provides the Q object that will be
used for the objects retrival with filter/get queryset methods.
Technically, neither args nor kwargs have to be provided, however the default
behaviour is to map all kwargs as the query constructors so that if this
method is not overriden only kwargs keys being model fields are valid.
If positional args are provided, the last one argument is understood
as the primary key. However this usage should be considered
deperecated, and will be removed in a future version.
"""
"""
tmp
=
dict
(
kwargs
)
kwargs
=
dict
(
kwargs
)
# If the URLconf includes a .(?P<format>\w+) pattern to match against
# If the URLconf includes a .(?P<format>\w+) pattern to match against
# a .json, .xml suffix, then drop the 'format' kwarg before
# a .json, .xml suffix, then drop the 'format' kwarg before
# constructing the query.
# constructing the query.
if
BaseRenderer
.
_FORMAT_QUERY_PARAM
in
tmp
:
if
BaseRenderer
.
_FORMAT_QUERY_PARAM
in
kwargs
:
del
tmp
[
BaseRenderer
.
_FORMAT_QUERY_PARAM
]
del
kwargs
[
BaseRenderer
.
_FORMAT_QUERY_PARAM
]
return
Q
(
**
tmp
)
return
kwargs
def
get_instance_data
(
self
,
model
,
content
,
**
kwargs
):
def
get_instance_data
(
self
,
model
,
content
,
**
kwargs
):
"""
"""
Returns the dict with the data for model instance creation/update
query
.
Returns the dict with the data for model instance creation/update.
Arguments:
Arguments:
- model: model class (django.db.models.Model subclass) to work with
- model: model class (django.db.models.Model subclass) to work with
...
@@ -536,12 +521,11 @@ class ModelMixin(object):
...
@@ -536,12 +521,11 @@ class ModelMixin(object):
return
all_kw_args
return
all_kw_args
def
get_
object
(
self
,
*
args
,
**
kwargs
):
def
get_
instance
(
self
,
**
kwargs
):
"""
"""
Get
the instance object
for read/update/delete requests.
Get
a model instance
for read/update/delete requests.
"""
"""
model
=
self
.
resource
.
model
return
self
.
get_queryset
()
.
get
(
**
kwargs
)
return
model
.
objects
.
get
(
self
.
build_query
(
*
args
,
**
kwargs
))
def
get_queryset
(
self
):
def
get_queryset
(
self
):
"""
"""
...
@@ -563,21 +547,15 @@ class ReadModelMixin(ModelMixin):
...
@@ -563,21 +547,15 @@ class ReadModelMixin(ModelMixin):
"""
"""
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
model
=
self
.
resource
.
model
model
=
self
.
resource
.
model
query_kwargs
=
self
.
get_query_kwargs
(
request
,
*
args
,
**
kwargs
)
try
:
try
:
self
.
model_instance
=
self
.
get_
object
(
*
args
,
**
kwargs
)
self
.
model_instance
=
self
.
get_
instance
(
**
query_
kwargs
)
except
model
.
DoesNotExist
:
except
model
.
DoesNotExist
:
raise
ErrorResponse
(
status
.
HTTP_404_NOT_FOUND
)
raise
ErrorResponse
(
status
.
HTTP_404_NOT_FOUND
)
return
self
.
model_instance
return
self
.
model_instance
def
build_query
(
self
,
*
args
,
**
kwargs
):
# Build query is overriden to filter the kwargs priori
# to use them as build_query argument
filtered_keywords
=
kwargs
.
copy
()
return
super
(
ReadModelMixin
,
self
)
.
build_query
(
*
args
,
**
filtered_keywords
)
class
CreateModelMixin
(
ModelMixin
):
class
CreateModelMixin
(
ModelMixin
):
"""
"""
...
@@ -625,11 +603,12 @@ class UpdateModelMixin(ModelMixin):
...
@@ -625,11 +603,12 @@ class UpdateModelMixin(ModelMixin):
"""
"""
def
put
(
self
,
request
,
*
args
,
**
kwargs
):
def
put
(
self
,
request
,
*
args
,
**
kwargs
):
model
=
self
.
resource
.
model
model
=
self
.
resource
.
model
query_kwargs
=
self
.
get_query_kwargs
(
request
,
*
args
,
**
kwargs
)
# TODO: update on the url of a non-existing resource url doesn't work
# 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
# 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_
instance
(
*
query_
kwargs
)
for
(
key
,
val
)
in
self
.
CONTENT
.
items
():
for
(
key
,
val
)
in
self
.
CONTENT
.
items
():
setattr
(
self
.
model_instance
,
key
,
val
)
setattr
(
self
.
model_instance
,
key
,
val
)
...
@@ -645,9 +624,10 @@ class DeleteModelMixin(ModelMixin):
...
@@ -645,9 +624,10 @@ class DeleteModelMixin(ModelMixin):
"""
"""
def
delete
(
self
,
request
,
*
args
,
**
kwargs
):
def
delete
(
self
,
request
,
*
args
,
**
kwargs
):
model
=
self
.
resource
.
model
model
=
self
.
resource
.
model
query_kwargs
=
self
.
get_query_kwargs
(
request
,
*
args
,
**
kwargs
)
try
:
try
:
instance
=
self
.
get_
object
(
*
args
,
**
kwargs
)
instance
=
self
.
get_
instance
(
**
query_
kwargs
)
except
model
.
DoesNotExist
:
except
model
.
DoesNotExist
:
raise
ErrorResponse
(
status
.
HTTP_404_NOT_FOUND
,
None
,
{})
raise
ErrorResponse
(
status
.
HTTP_404_NOT_FOUND
,
None
,
{})
...
@@ -663,8 +643,9 @@ class ListModelMixin(ModelMixin):
...
@@ -663,8 +643,9 @@ class ListModelMixin(ModelMixin):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
queryset
=
self
.
get_queryset
()
queryset
=
self
.
get_queryset
()
ordering
=
self
.
get_ordering
()
ordering
=
self
.
get_ordering
()
query_kwargs
=
self
.
get_query_kwargs
(
request
,
*
args
,
**
kwargs
)
queryset
=
queryset
.
filter
(
self
.
build_query
(
**
kwargs
)
)
queryset
=
queryset
.
filter
(
**
query_kwargs
)
if
ordering
:
if
ordering
:
queryset
=
queryset
.
order_by
(
*
ordering
)
queryset
=
queryset
.
order_by
(
*
ordering
)
...
...
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