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.
This commit is contained in:
Sean C. Farley 2012-03-05 20:13:26 -05:00
parent 55317b0372
commit 02b2020995

View File

@ -3,6 +3,7 @@ The :mod:`mixins` module provides a set of reusable `mixin`
classes that can be added to a `View`. classes that can be added to a `View`.
""" """
from collections import Callable
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
@ -513,6 +514,8 @@ class ModelMixin(object):
""" """
Return the queryset for this view. Return the queryset for this view.
""" """
queryset = getattr(self.resource, 'queryset',
self.resource.model.objects.all())
return getattr(self.resource, 'queryset', return getattr(self.resource, 'queryset',
self.resource.model.objects.all()) self.resource.model.objects.all())