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
dc45bc7b
Commit
dc45bc7b
authored
Apr 09, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add lookup_kwarg
parent
099163f8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
9 deletions
+15
-9
rest_framework/generics.py
+12
-6
rest_framework/tests/filterset.py
+3
-3
No files found.
rest_framework/generics.py
View file @
dc45bc7b
...
@@ -26,6 +26,7 @@ class GenericAPIView(views.APIView):
...
@@ -26,6 +26,7 @@ class GenericAPIView(views.APIView):
pagination_serializer_class
=
api_settings
.
DEFAULT_PAGINATION_SERIALIZER_CLASS
pagination_serializer_class
=
api_settings
.
DEFAULT_PAGINATION_SERIALIZER_CLASS
allow_empty
=
True
allow_empty
=
True
page_kwarg
=
'page'
page_kwarg
=
'page'
lookup_kwarg
=
'pk'
# Pending deprecation
# Pending deprecation
model
=
None
model
=
None
...
@@ -167,23 +168,26 @@ class GenericAPIView(views.APIView):
...
@@ -167,23 +168,26 @@ class GenericAPIView(views.APIView):
By default this requires `self.queryset` and a `pk` or `slug` argument
By default this requires `self.queryset` and a `pk` or `slug` argument
in the URLconf, but subclasses can override this to return any object.
in the URLconf, but subclasses can override this to return any object.
"""
"""
# Use a custom queryset if provided; this is required for subclasses
# Determine the base queryset to use.
# like DateDetailView
if
queryset
is
None
:
if
queryset
is
None
:
queryset
=
self
.
get_queryset
()
queryset
=
self
.
get_queryset
()
# Next, try looking up by primary key.
# Perform the lookup filtering.
pk
=
self
.
kwargs
.
get
(
self
.
pk_url_kwarg
,
None
)
pk
=
self
.
kwargs
.
get
(
self
.
pk_url_kwarg
,
None
)
slug
=
self
.
kwargs
.
get
(
self
.
slug_url_kwarg
,
None
)
slug
=
self
.
kwargs
.
get
(
self
.
slug_url_kwarg
,
None
)
if
pk
is
not
None
:
lookup
=
self
.
kwargs
.
get
(
self
.
lookup_kwarg
,
None
)
if
lookup
is
not
None
:
queryset
=
queryset
.
filter
(
**
{
self
.
lookup_kwarg
:
lookup
})
elif
pk
is
not
None
:
queryset
=
queryset
.
filter
(
pk
=
pk
)
queryset
=
queryset
.
filter
(
pk
=
pk
)
# Next, try looking up by slug.
elif
slug
is
not
None
:
elif
slug
is
not
None
:
queryset
=
queryset
.
filter
(
**
{
self
.
slug_field
:
slug
})
queryset
=
queryset
.
filter
(
**
{
self
.
slug_field
:
slug
})
# If none of those are defined, it's an error.
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
:
try
:
# Get the single item from the filtered queryset
# Get the single item from the filtered queryset
obj
=
queryset
.
get
()
obj
=
queryset
.
get
()
...
@@ -191,7 +195,9 @@ class GenericAPIView(views.APIView):
...
@@ -191,7 +195,9 @@ class GenericAPIView(views.APIView):
raise
Http404
(
_
(
"No
%(verbose_name)
s found matching the query"
)
%
raise
Http404
(
_
(
"No
%(verbose_name)
s found matching the query"
)
%
{
'verbose_name'
:
queryset
.
model
.
_meta
.
verbose_name
})
{
'verbose_name'
:
queryset
.
model
.
_meta
.
verbose_name
})
# May raise a permission denied
self
.
check_object_permissions
(
self
.
request
,
obj
)
self
.
check_object_permissions
(
self
.
request
,
obj
)
return
obj
return
obj
...
...
rest_framework/tests/filterset.py
View file @
dc45bc7b
...
@@ -61,7 +61,7 @@ if django_filters:
...
@@ -61,7 +61,7 @@ if django_filters:
class
CommonFilteringTestCase
(
TestCase
):
class
CommonFilteringTestCase
(
TestCase
):
def
_serialize_object
(
self
,
obj
):
def
_serialize_object
(
self
,
obj
):
return
{
'id'
:
obj
.
id
,
'text'
:
obj
.
text
,
'decimal'
:
obj
.
decimal
,
'date'
:
obj
.
date
}
return
{
'id'
:
obj
.
id
,
'text'
:
obj
.
text
,
'decimal'
:
obj
.
decimal
,
'date'
:
obj
.
date
}
def
setUp
(
self
):
def
setUp
(
self
):
"""
"""
Create 10 FilterableItem instances.
Create 10 FilterableItem instances.
...
@@ -190,7 +190,7 @@ class IntegrationTestDetailFiltering(CommonFilteringTestCase):
...
@@ -190,7 +190,7 @@ class IntegrationTestDetailFiltering(CommonFilteringTestCase):
Integration tests for filtered detail views.
Integration tests for filtered detail views.
"""
"""
urls
=
'rest_framework.tests.filterset'
urls
=
'rest_framework.tests.filterset'
def
_get_url
(
self
,
item
):
def
_get_url
(
self
,
item
):
return
reverse
(
'detail-view'
,
kwargs
=
dict
(
pk
=
item
.
pk
))
return
reverse
(
'detail-view'
,
kwargs
=
dict
(
pk
=
item
.
pk
))
...
@@ -221,7 +221,7 @@ class IntegrationTestDetailFiltering(CommonFilteringTestCase):
...
@@ -221,7 +221,7 @@ class IntegrationTestDetailFiltering(CommonFilteringTestCase):
response
=
self
.
client
.
get
(
'{url}?decimal={param}'
.
format
(
url
=
self
.
_get_url
(
low_item
),
param
=
search_decimal
))
response
=
self
.
client
.
get
(
'{url}?decimal={param}'
.
format
(
url
=
self
.
_get_url
(
low_item
),
param
=
search_decimal
))
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
data
,
low_item_data
)
self
.
assertEqual
(
response
.
data
,
low_item_data
)
# Tests that multiple filters works.
# Tests that multiple filters works.
search_decimal
=
Decimal
(
'5.25'
)
search_decimal
=
Decimal
(
'5.25'
)
search_date
=
datetime
.
date
(
2012
,
10
,
2
)
search_date
=
datetime
.
date
(
2012
,
10
,
2
)
...
...
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