django-ninja
django-ninja copied to clipboard
Whether django-ninja has a caching mechanism, and whether django's cache configuration is valid for ninja.
Whether django-ninja has a caching mechanism, and whether django's cache configuration is valid for ninja.
a quick search reveals that django-ninja has no mention of caching.
But you can use basic caching within the api-functions as described in the django docs: https://docs.djangoproject.com/en/4.1/topics/cache/#basic-usage
a quick search reveals that django-ninja has no mention of caching.
But you can use basic caching within the api-functions as described in the django docs: https://docs.djangoproject.com/en/4.1/topics/cache/#basic-usage
After testing, the set cache is invalid and the code reports an error
from django.views.decorators.cache import never_cache, cache_page
@router.get("/movielist/{movie_type_id}", response=List[MovieSchemaOut])
@paginate
@cache_page(20)
def list_movie(request, movie_type_id: int):
a = MovieType.objects.get(id=movie_type_id)
qs = a.movie_set.filter(Q(production_country__contains="CN")|Q(production_country__contains="HK"))
return qs
Error message
Traceback (most recent call last):
File "D:\Program Files (x86)\Anaconda3\envs\django\lib\site-packages\ninja\operation.py", line 99, in run
result = self.view_func(request, **values)
File "D:\Program Files (x86)\Anaconda3\envs\django\lib\site-packages\ninja\pagination.py", line 143, in view_with_pagination
items = func(*args, **kwargs)
File "D:\Program Files (x86)\Anaconda3\envs\django\lib\site-packages\django\utils\decorators.py", line 148, in _wrapped_view
return middleware.process_response(request, response)
File "D:\Program Files (x86)\Anaconda3\envs\django\lib\site-packages\django\middleware\cache.py", line 83, in process_response
if response.streaming or response.status_code not in (200, 304):
AttributeError: 'QuerySet' object has no attribute 'streaming'
@ddzyx Strange, but it looks like ninja
is not compatible with Django middleware (isn't it what ninja suppose to solve in the first place?).
Your views should return the JsonResponse
object in order to make it work.
is it possible to use cache_page
from django together with ninja?
If anyone lands on this, you can use a combination of ninja.decorators.decorate_view
& django.views.decorators.cache.cache_page
from django.views.decorators.cache import cache_page
from ninja.decorators import decorate_view
@api.get("/")
@decorate_view(cache_page(10))
def info(request):
...