Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Django REST Framework caching?

I'm just start working with django and DRF, and occure a problem, that is looks like DRF cache responses. I mean - I can change object, create new, or delete it - and DRF keep response, thats nothing is changed. For example, I create an object, but modelViewSet still return data where this object does not presented. But if I directly request it object - it show that it's created. And so with any another actions. I can't find topic about caching in DRF, and look like I have not any django chaching middlewares, so I have no idea what is going on. Only one thing that helps - restart server ( I'm using default dev-server).

One more thing - all data is ok when it's rendered by django views, not DRF views.

Here is one of the serializers/modelViewSets that I'm using. It simple as it possible. And also - I'm not using django cache backends. At least - I have not any in my settings.

class WorkOperationSerializer(serializers.ModelSerializer):
    class Meta:
        model = WorkOperation


class WorkOperationAPIView(viewsets.ModelViewSet):
    serializer_class = WorkOperationSerializer
    queryset = WorkOperation.objects.all()

    def get_queryset(self):
        return self.queryset
like image 647
GeraldIstar Avatar asked Sep 21 '15 13:09

GeraldIstar


People also ask

How do I disable caching in Django?

structure, setting CACHES = None or CACHES['default']['BACKEND'] = None causes Django to choke, and setting CACHES = {} still seems to enable basic caching.

Does Django automatically cache?

Unless we explicitly specify another caching method in our settings file, Django defaults to local memory caching. As its name implies, this method stores cached data in RAM on the machine where Django is running. Local memory caching is fast, responsive, and thread-safe.

How does Django handle cache?

To use cache in Django, first thing to do is to set up where the cache will stay. The cache framework offers different possibilities - cache can be saved in database, on file system or directly in memory. Setting is done in the settings.py file of your project.


1 Answers

You can read here about django queryset caching. The best advice seems to be: re-run the .all() method to get fresh results. Using object.property may give you cached results.

like image 102
allo Avatar answered Sep 18 '22 14:09

allo