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
e0020c5b
Commit
e0020c5b
authored
Apr 11, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify get_object
parent
07af4373
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
11 deletions
+9
-11
rest_framework/generics.py
+9
-11
No files found.
rest_framework/generics.py
View file @
e0020c5b
...
@@ -4,9 +4,10 @@ Generic views that provide commonly needed behaviour.
...
@@ -4,9 +4,10 @@ Generic views that provide commonly needed behaviour.
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
from
rest_framework
import
views
,
mixins
from
rest_framework
import
views
,
mixins
from
rest_framework.settings
import
api_settings
from
rest_framework.settings
import
api_settings
from
django.core.exceptions
import
ImproperlyConfigured
,
ObjectDoesNotExist
from
django.core.exceptions
import
ImproperlyConfigured
from
django.core.paginator
import
Paginator
,
InvalidPage
from
django.core.paginator
import
Paginator
,
InvalidPage
from
django.http
import
Http404
from
django.http
import
Http404
from
django.shortcuts
import
get_object_or_404
from
django.utils.translation
import
ugettext
as
_
from
django.utils.translation
import
ugettext
as
_
### Base classes for the generic views ###
### Base classes for the generic views ###
...
@@ -163,7 +164,7 @@ class GenericAPIView(views.APIView):
...
@@ -163,7 +164,7 @@ class GenericAPIView(views.APIView):
# TODO: Deprecation warning
# TODO: Deprecation warning
return
self
.
model
.
_default_manager
.
all
()
return
self
.
model
.
_default_manager
.
all
()
raise
ImproperlyConfigured
(
"'
%
s' must define 'queryset'
or 'model'
"
raise
ImproperlyConfigured
(
"'
%
s' must define 'queryset'"
%
self
.
__class__
.
__name__
)
%
self
.
__class__
.
__name__
)
def
get_object
(
self
,
queryset
=
None
):
def
get_object
(
self
,
queryset
=
None
):
...
@@ -177,6 +178,8 @@ class GenericAPIView(views.APIView):
...
@@ -177,6 +178,8 @@ class GenericAPIView(views.APIView):
# Determine the base queryset to use.
# Determine the base queryset to use.
if
queryset
is
None
:
if
queryset
is
None
:
queryset
=
self
.
filter_queryset
(
self
.
get_queryset
())
queryset
=
self
.
filter_queryset
(
self
.
get_queryset
())
else
:
pass
# Deprecation warning
# Perform the lookup filtering.
# Perform the lookup filtering.
pk
=
self
.
kwargs
.
get
(
self
.
pk_url_kwarg
,
None
)
pk
=
self
.
kwargs
.
get
(
self
.
pk_url_kwarg
,
None
)
...
@@ -184,24 +187,19 @@ class GenericAPIView(views.APIView):
...
@@ -184,24 +187,19 @@ class GenericAPIView(views.APIView):
lookup
=
self
.
kwargs
.
get
(
self
.
lookup_kwarg
,
None
)
lookup
=
self
.
kwargs
.
get
(
self
.
lookup_kwarg
,
None
)
if
lookup
is
not
None
:
if
lookup
is
not
None
:
queryset
=
queryset
.
filter
(
**
{
self
.
lookup_kwarg
:
lookup
})
filter_kwargs
=
{
self
.
lookup_kwarg
:
lookup
}
elif
pk
is
not
None
:
elif
pk
is
not
None
:
# TODO: Deprecation warning
# TODO: Deprecation warning
queryset
=
queryset
.
filter
(
pk
=
pk
)
filter_kwargs
=
{
'pk'
:
pk
}
elif
slug
is
not
None
:
elif
slug
is
not
None
:
# TODO: Deprecation warning
# TODO: Deprecation warning
queryset
=
queryset
.
filter
(
**
{
self
.
slug_field
:
slug
})
filter_kwargs
=
{
self
.
slug_field
:
slug
}
else
:
else
:
raise
AttributeError
(
"Generic detail view
%
s must be called with "
raise
AttributeError
(
"Generic detail view
%
s must be called with "
"either an object pk or a slug."
"either an object pk or a slug."
%
self
.
__class__
.
__name__
)
%
self
.
__class__
.
__name__
)
try
:
obj
=
get_object_or_404
(
queryset
,
**
filter_kwargs
)
# Get the single item from the filtered queryset
obj
=
queryset
.
get
()
except
ObjectDoesNotExist
:
raise
Http404
(
_
(
"No
%(verbose_name)
s found matching the query"
)
%
{
'verbose_name'
:
queryset
.
model
.
_meta
.
verbose_name
})
# May raise a permission denied
# May raise a permission denied
self
.
check_object_permissions
(
self
.
request
,
obj
)
self
.
check_object_permissions
(
self
.
request
,
obj
)
...
...
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