Hassan Baig
Hassan Baig

Reputation: 15814

Per-view caching for get_queryset method in a class-based view (Django app)

TL;DR:

I can't seem to apply per-view caching to a class-based view's get_queryset method. Is there any alternative way I can use to apply caching to a class-based view's method? Or, is there no way at all?

Currently, the error I'm getting is: 'View' object has no attribute 'method'. If I write the same decorator on top of the view, I get: 'function' object has no attribute 'as_view' (traceback below).


Details:

Here's the view:

#@cache_page(20)
class OnlineView(ListView):
    template_name = "online.html"
    paginate_by = 75

    #@cache_page(20)   
    def get_queryset(self):
        users = Session.objects.filter(last_activity__gte=(timezone.now()-timedelta(minutes=5))).only('user').distinct('user')
        return users

Here's the traceback I get when I write the caching tag on top of the view:

Traceback (most recent call last):
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 92, in inner_run
    self.validate(display_num_errors=True)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/management/base.py", line 280, in validate
    num_errors = get_validation_errors(s, app)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/management/validation.py", line 35, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/loading.py", line 166, in get_app_errors
    self._populate()
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/loading.py", line 75, in _populate
    self.load_app(app_name)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/loading.py", line 96, in load_app
    models = import_module('.models', app_name)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/debug_toolbar/models.py", line 63, in <module>
    patch_root_urlconf()
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/debug_toolbar/models.py", line 51, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 467, in reverse
    app_list = resolver.app_dict[ns]
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 311, in app_dict
    self._populate()
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 263, in _populate
    for pattern in reversed(self.url_patterns):
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 347, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 342, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/hassan/Desktop/unconnectedredditpk/unconnectedreddit/urls.py", line 26, in <module>
    url(r'^online/$', auth(OnlineView.as_view()), name='online'),
AttributeError: 'function' object has no attribute 'as_view'

Lastly, here's the pertinent thing I have in urls.py:

url(r'^online/$', auth(OnlineView.as_view()), name='online'),

Upvotes: 0

Views: 1692

Answers (1)

solarissmoke
solarissmoke

Reputation: 31404

It will be easier for you to achieve this by adding the caching in your URL config:

from django.views.decorators.cache import cache_page

url(r'^online/$', cache_page(20)(auth(OnlineView.as_view())), name='online'),

However I notice you have an auth() decorator on the same view. I'm not sure you want to be caching a view that requires authentication...

Upvotes: 1

Related Questions