我正在 DRF 中提供产品列表,并尝试在 ClassBasedApiViews、
urls.py中使用缓存
path('',ProductListAPIView.as_view(), name='products')
Run Code Online (Sandbox Code Playgroud)
视图.py:
class ProductListAPIView(ListAPIView):
permission_classes = [IsAuthenticated]
queryset = Product.objects.all()
serializer_class = ProductSerializer
Run Code Online (Sandbox Code Playgroud)
序列化器.py:
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = '__all__'
depth = 1
Run Code Online (Sandbox Code Playgroud)
没有像这样的函数def get() ...,所以我不能在函数之前使用传统的装饰器来缓存。
我试过的是path('', cache_page(300)(ProductListAPIView.as_view()), name='products')这个有效。
如果有多个装饰器path('', vary_on_headers('Authorization')(cache_page(300)(ProductListAPIView.as_view())), name='products')也可以。
有没有更好的方法来使用装饰器cache_page和vary_on_headersClassBasedAPIView?
python django caching django-class-based-views django-rest-framework