Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using @cache_response() decorator from drf extensions while using @api_view function view

I want to cache my API responses. I am using Django Rest Framework and want use for this purpose @cache_response() decorator from drf extensions.

I am following examples from: http://chibisov.github.io/drf-extensions/docs/#cache-response There are only examples of class based views.

When I apply it like this:

@api_view(http_method_names=['GET'])
@cache_response()
def my_view(request, some_arg):
    (...)

I end up with error:

inner() takes at least 2 arguments (1 given)

Full traceback:

Traceback:

    File "/Users/andi/.virtualenvs/adfoo/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
      112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/Users/andi/.virtualenvs/adfoo/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
      57.         return view_func(*args, **kwargs)
    File "/Users/andi/.virtualenvs/adfoo/lib/python2.7/site-packages/django/views/generic/base.py" in view
      69.             return self.dispatch(request, *args, **kwargs)
    File "/Users/andi/.virtualenvs/adfoo/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
      407.             response = self.handle_exception(exc)
    File "/Users/andi/.virtualenvs/adfoo/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
      404.             response = handler(request, *args, **kwargs)
    File "/Users/andi/.virtualenvs/adfoo/lib/python2.7/site-packages/rest_framework/decorators.py" in handler
      51.             return func(*args, **kwargs)

    Exception Type: TypeError at /api/foo/1/exhibitors/
    Exception Value: inner() takes at least 2 arguments (1 given)

How should I apply it correctly?

DRF extensions specifies for the decorator following requirenets for decorated method:

1) It should be method of class which is inherited from rest_framework.views.APIView

2) It should return rest_framework.response.Response instance.

taking into account point 1) I am not sure if its at all possible..

here is the source of drf @api_view decorator https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/decorators.py

and here is the source of drf extension @cache_response decorator https://github.com/chibisov/drf-extensions/blob/master/rest_framework_extensions/cache/decorators.py

like image 254
andilabs Avatar asked Nov 16 '25 02:11

andilabs


1 Answers

As Ashish mentioned cache_response decorator of drf-extensions is used for methods of APIView classes. But I wouldn't recommend it even in that case. Because Django already has support for using function decorators for methods.

So there are two way:

1 - Use Django function views with Django Rest Framework api_view decorator and Django cache_page decorator.

from django.views.decorators.cache import cache_page
from rest_framework.decorators import api_view


@cache_page(60 * 15)
@api_view(['GET', 'POST'])
def snippet_list(request):
    ...

2 - Use Django Rest Framework class based views with Django method_decorator decorator.

from django.views.decorators.cache import cache_page
from django.utils.decorators import method_decorator


@method_decorator(cache_page(60 * 15), name='dispatch')
class SnippetListView(APIView):
    ...

The same is true about never_cache, etc.

like image 131
Behzad B. Mokhtari Avatar answered Nov 17 '25 18:11

Behzad B. Mokhtari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!