Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable @cache_page for some of the Django Rest Framework views?

I have basic rest framework setup:

url(r'^items/$', ItemList.as_view(), name='item-list'),
...
class ItemList(generics.ListCreateAPIView):
    model = Item
    serializer_class = ItemSerializer

I want to cache this request using @cache_page decorator. I tried something stupid like:

url(r'^items/$', cached_items, name='item-list'),
...
@cache_page(1000)
def cached_items(request):
    return ItemList.as_view()

which doesn't work. How can I wrap those views properly?

like image 279
serg Avatar asked Mar 20 '15 20:03

serg


1 Answers

With the same decorator you can use in the url patterns with class view as simple view (using .as_view method)

from django.views.decorators.cache import cache_page

urlpatterns = ('',
    url(r'^items/$', cache_page(60 * 60)(ItemList.as_view()), name='item-list')
)
like image 142
yograterol Avatar answered Oct 05 '22 12:10

yograterol