mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-12-04 07:24:03 +03:00
Added support for filtering via query parameters
This commit is contained in:
parent
87a9072b56
commit
9141171ba0
|
@ -33,6 +33,8 @@ __all__ = (
|
||||||
'UpdateModelMixin',
|
'UpdateModelMixin',
|
||||||
'DeleteModelMixin',
|
'DeleteModelMixin',
|
||||||
'ListModelMixin'
|
'ListModelMixin'
|
||||||
|
'FilterModelMixin'
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -182,7 +184,7 @@ class RequestMixin(object):
|
||||||
return parser.parse(stream)
|
return parser.parse(stream)
|
||||||
|
|
||||||
raise ErrorResponse(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
|
raise ErrorResponse(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
|
||||||
{'error': 'Unsupported media type in request \'%s\'.' %
|
{'error': 'Unsupported media type in request \'%s\'.' %
|
||||||
content_type})
|
content_type})
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -659,6 +661,52 @@ class ListModelMixin(ModelMixin):
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
|
class FilterModelMixin(ModelMixin):
|
||||||
|
"""
|
||||||
|
Behavior to list a set of `model` instances on GET requests
|
||||||
|
Also Enables a filter via the url query parameters
|
||||||
|
|
||||||
|
Example: Each of the following requests are valid
|
||||||
|
1) http://www.example.com/?color=blue
|
||||||
|
2) http://www.example.com/shirts/?color=blue
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
queryset = self.get_queryset()
|
||||||
|
ordering = self.get_ordering()
|
||||||
|
|
||||||
|
|
||||||
|
filter_data = dict(request.GET.items())
|
||||||
|
|
||||||
|
# Get the fields for the resource
|
||||||
|
model = self.resource.model
|
||||||
|
fields = model._meta.fields
|
||||||
|
|
||||||
|
# Iterate over fields and get the field names
|
||||||
|
fieldnames = [field.name for field in fields]
|
||||||
|
|
||||||
|
|
||||||
|
# Initialize query object
|
||||||
|
filter_query = Q()
|
||||||
|
|
||||||
|
# Check to ensure that each filter key is a valid field in the model
|
||||||
|
for filter_key,value in filter_data.iteritems():
|
||||||
|
if filter_key in fieldnames:
|
||||||
|
new_filter = {filter_key:value}
|
||||||
|
new_query = Q(**new_filter)
|
||||||
|
|
||||||
|
filter_query = filter_query & new_query
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
queryset = queryset.filter(self.build_query(**kwargs)).filter(filter_query)
|
||||||
|
if ordering:
|
||||||
|
queryset = queryset.order_by(*ordering)
|
||||||
|
|
||||||
|
return queryset
|
||||||
|
|
||||||
########## Pagination Mixins ##########
|
########## Pagination Mixins ##########
|
||||||
|
|
||||||
class PaginatorMixin(object):
|
class PaginatorMixin(object):
|
||||||
|
|
|
@ -24,6 +24,7 @@ __all__ = (
|
||||||
'InstanceModelView',
|
'InstanceModelView',
|
||||||
'ListModelView',
|
'ListModelView',
|
||||||
'ListOrCreateModelView'
|
'ListOrCreateModelView'
|
||||||
|
'FilterOrCreateModelView'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -296,3 +297,12 @@ class ListOrCreateModelView(ListModelMixin, CreateModelMixin, ModelView):
|
||||||
A view which provides default operations for list and create, against a model in the database.
|
A view which provides default operations for list and create, against a model in the database.
|
||||||
"""
|
"""
|
||||||
_suffix = 'List'
|
_suffix = 'List'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class FilterOrCreateModelView(FilterModelMixin, CreateModelMixin, ModelView):
|
||||||
|
"""
|
||||||
|
A view which provides default operations for list and create, against a model in the database.
|
||||||
|
The view also enables filtering by query parameters
|
||||||
|
"""
|
||||||
|
_suffix = 'List'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user