Django 使用DRF缓存视图集:TypeError:_-wrapped_-view()
我只想对速度太慢的视图集使用缓存:(,使用Django REST Framework) 我已经做到了:Django 使用DRF缓存视图集:TypeError:_-wrapped_-view(),django,caching,django-rest-framework,memcached,Django,Caching,Django Rest Framework,Memcached,我只想对速度太慢的视图集使用缓存:(,使用Django REST Framework) 我已经做到了: ... from django.utils.decorators import method_decorator from django.views.decorators.cache import cache_page from django.views.decorators.vary import vary_on_cookie ... class PRPKViewSet(viewsets.M
...
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.decorators.vary import vary_on_cookie
...
class PRPKViewSet(viewsets.ModelViewSet):
serializer_class = PrpkSerializer
queryset = Prpk.objects.all().order_by('begin')
# Authentification !
permission_classes = (IsAuthenticated,)
# Only 'get' method
http_method_names = ['get']
# Cache requested url for each user for 2 hours
# @method_decorator(vary_on_cookie)
@method_decorator(cache_page(60*2))
def get_queryset(self):
""" allow rest api to filter by submissions """
queryset = Prpk.objects.all().order_by('begin')
highway = self.request.query_params.get('highway', None)
if highway is not None:
queryset = queryset.filter(highway=highway)
return queryset
但在查询时,我发现以下错误:
TypeError: _wrapped_view() missing 1 required positional argument: 'request'
已安装Memcached
那么,我是否可以只缓存一个视图集(不使用扩展?)
非常感谢
F.装饰
调度
而不是获取查询集
@method_decorator(cache_page(60*2))
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
您好,谢谢。我刚刚在“get_queryset”上面添加了“dispatch”函数,但我有一个错误:“if response.streaming或response.status_代码不在(200304)中:AttributeError:'NoneType'对象没有属性'streaming'”“my bad:缺少”return:)。第一次查询:11秒,第二次呼叫:431毫秒:)。它起作用了。现在,我怎样才能看到当前缓存的页面呢?@fabrice true,我错过了
return
,修复了。查看查看如何获得错误页面,因为这是一个不同的问题。