From 02b2020995c6a59063fbab665bf0abac75244439 Mon Sep 17 00:00:00 2001 From: "Sean C. Farley" Date: Mon, 5 Mar 2012 20:13:26 -0500 Subject: [PATCH 1/2] Handle a callable queryset in ModelMixin To allow queryset's that may depend on a value obtained from a middleware or somewhere else that can change the result of the query, accept and process a callable queryset in addition to previous behavior. --- djangorestframework/mixins.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 6c8f8179d..8977ccb68 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -3,6 +3,7 @@ The :mod:`mixins` module provides a set of reusable `mixin` classes that can be added to a `View`. """ +from collections import Callable from django.contrib.auth.models import AnonymousUser from django.core.paginator import Paginator from django.db.models.fields.related import ForeignKey @@ -513,6 +514,8 @@ class ModelMixin(object): """ Return the queryset for this view. """ + queryset = getattr(self.resource, 'queryset', + self.resource.model.objects.all()) return getattr(self.resource, 'queryset', self.resource.model.objects.all()) From c3cb70ac965d8ee6d26d15d07f5fe09437f03a03 Mon Sep 17 00:00:00 2001 From: "Sean C. Farley" Date: Mon, 5 Mar 2012 20:36:33 -0500 Subject: [PATCH 2/2] Add missing piece to commit 02b2020995 I missed copying this change from the test environment. --- djangorestframework/mixins.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 8977ccb68..c1667b02f 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -516,8 +516,8 @@ class ModelMixin(object): """ queryset = getattr(self.resource, 'queryset', self.resource.model.objects.all()) - return getattr(self.resource, 'queryset', - self.resource.model.objects.all()) + return queryset() if isinstance(queryset, Callable) else queryset + def get_ordering(self): """