Technology, programming, python, work and interesting things

Django

Django generic views cache-like behavior

In certain cases the Django generic views will behave like they are caching data. They are doing that for the queryset argument and not for extra_context which is known to be cached. This will happen if you send to the generic view a queryset filtered with a callable.

Let's take as an example a blogging application. In blog.models we have:

class PublishedManager(Manager): 
def get_query_set(self):
queryset = super(PublishedManager, self).get_query_set()
return queryset.filter(pub_date__lte=datetime.now)

class Entry(models.Model):
...
pub_date = models.DateTimeField()
published = PublishedManager()
...

In blog.urls:

info_dict = {'queryset': Entry.published.all()} 

entry_list = url( regex = '^$',
view ...
Copyright © 2008-2011 Mihail Ovidiu Pascut