, ItemList.as_view...">

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

I have a basic setup of a rest environment:

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_pagedecorator. I tried something stupid:

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

which does not work. How can I wrap these views properly?

+4
source share
1 answer

Using the same decorator, you can use urls in the templates to represent the class as a simple view (using the method .as_view)

from django.views.decorators.cache import cache_page

urlpatterns = ('',
    url(r'^items/$', cache_page(60 * 60)(ItemList.as_view()), name='item-list')
)
+6
source

All Articles