Blake Gibbs
Blake Gibbs

Reputation: 495

Saving data on server side using REST

I'm using django rest framework and trying to save some data so it will be accessible by GET, PUT, DELETE.

So when user send GET request server send some information (a random number, for example) and that information is needed after user sends PUT request on the same url. How would one save such information? I'm using class-based views.

So i want to save that information on GET method.

  1. I tried saving that information to class variable self.information, but the problem is self.information is empty when PUT method is getting called.

  2. I also tried saving it to session, but like class variable, session is also empty when PUT method is being executed.

    class SampleClass(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, generics.GenericAPIView):
        serializer_class = SampleSerializer
    
    def get(self, request):
        random_number = random.randint(0, 10)
        request.session['number'] = random_number;
        content = {'random_number': random_number}
        return Response(content)
    
    def put(self, request):
        number = request.session['number'] #  key doesn't exists
        process_number(number)
        # ...
    

Upvotes: 1

Views: 1949

Answers (1)

Kevin Brown-Silva
Kevin Brown-Silva

Reputation: 41671

Before I begin, it's important to note that HTTP is a stateless protocol, and you are looking to add state into the mix. If you can rework what you are doing to not depend on previous requests, that will probably be better in the long run.


I tried saving that information to class variable self.information, but the problem is self.information is empty when PUT method is getting called.

This is because the class is re-initialized for each request. Because of that, the class variables don't persist across requests. Even if they did, that would mean everyone would get access to the persisted value, and it isn't made clear if that is what you are looking for.

I also tried saving it to session, but like class variable, session is also empty when PUT method is being executed

This doesn't work because Django sessions are persisted through the use of cookies. While this might work for SessionAuthentication, it won't work for any authentication that happens outside of the browser. This is because the session cookies won't be included, so Django will think the new requests are under a different session.


Now, just because HTTP is mostly stateless and doing this might lead to future trouble, that doesn't mean that you should never do it. The Django sessions wouldn't exist if there wasn't a need for it, and there are ways to save state without Django sessions.

  1. Create a new model for the state - This is usually the best way to save state per-user and ensure that it doesn't fade away. The model needs a user field along with the fields that the state will be stored in, and all you need to do is have a query that retrieves the state object for the user.
  2. Use the Django cache - This is the way I would recommend it for the case that you specified in your question. When you don't need to store much state, the state is shared among everyone, or you can live with it not existing (expiring), storing the data in a simple cache environment will probably work the best. You have much more control over what is stored, but at the expense of having to do more work.

Upvotes: 3

Related Questions