djangodjango-rest-frameworkdjango-cache

How to disable Django REST Framework caching?


I'm just startimg to work with django and DRF, and I got a problem, that looks like DRF cache responses. I mean - I can change object, create new, or delete it - and DRF shows in response, that nothing has changed. For example, I create an object, but modelViewSet still return data where the newly created object is not present. But if I directly request the object - it shows 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, but not with DRF views.

Here is one of the serializers/modelViewSets that I'm using. It as simple as possible. And also - I'm not using django cache backends. At least - I don#t 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

Solution

  • 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.