How to get Request.User in Django-Rest-Framework serializer?

I’ve tried something like this, it does not work. class PostSerializer(serializers.ModelSerializer): class Meta: model = Post def save(self): user = self.context[‘request.user’] title = self.validated_data[‘title’] article = self.validated_data[‘article’] I need a way of being able to access request.user from my Serializer class. 13 Answers 13

Django REST Framework: adding additional field to ModelSerializer

I want to serialize a model, but want to include an additional field that requires doing some database lookups on the model instance to be serialized: class FooSerializer(serializers.ModelSerializer): my_field = … # result of some database queries on the input Foo object class Meta: model = Foo fields = (‘id’, ‘name’, ‘myfield’) What is the … Read more

Django rest framework, use different serializers in the same ModelViewSet

I would like to provide two different serializers and yet be able to benefit from all the facilities of ModelViewSet: When viewing a list of objects, I would like each object to have an url which redirects to its details and every other relation appear using __unicode __ of the target model; example: { “url”: … Read more