Reputation: 1123
I want Django-RESTful API for a model in Django. But instead of storing the data in the database, I'd like to use the current session.
I've thought of making a custom ModelSerializer which overrides create() and update(). I've also thought about making a custom object manager such as:
MyModel.objects = SomeManager()
The problems is that I have to insert the request.session instance from the view to SomeManager(), but I'm not sure on where or how to do it in the best way.
Any tips?
Upvotes: 4
Views: 1869
Reputation: 9342
Instead of overriding create() and update() within serializer, you should look at overriding create() and update() of views. That is much better place for taking care of session variables.
You can even create your own class which extends APIView, CreateModelMixin and UpdateModelMixin. These two mixins will provide .update(request, *args, **kwargs) and .create(request, *args, **kwargs) methods, that implements updating and saving an existing model instance.
This documentation of DRF generic views gives nice idea about them: http://www.django-rest-framework.org/api-guide/generic-views/. Also, you can explore www.cdrf.co which has detailed descriptions, with full methods and attributes, for each of Django REST Framework's class-based views and serializers.
This is better place to handle temporary session variables until you want to save this data into persistent storage.
Upvotes: 1