1

I am developing an application in django-rest-framework. Following is my serializer:

class MySerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = My
        fields = ('user','title','description')

The problem is that when I run it, it asks for the user to be selected like this:

enter image description here

I want that whichever user is logged in, he should be added to user field automatically. In django website development, I used to do it using request.user but how do I do it in django rest framework?

sshussain270
  • 1,785
  • 4
  • 25
  • 49

3 Answers3

1

If you extend django view generic classes then you can add in views.py

from rest_framework.permissions import IsAuthenticate

class MyView(generics.ListCreateAPIView):
    permission_classes = (IsAuthenticated,)
    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

And then in serializers.py

class MySerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = My
        fields = ('user','title','description')
        read_only_fields = ('user',) # Add this
Prakash S
  • 632
  • 6
  • 12
  • I've tried to get the use in serialize.py but in vain. I had to modify my `MyPageViewSet(viewsets.ModelViewSet):` and add the `perform_create` function as above to make it work. – uak Apr 03 '22 at 13:17
0

A default class that can be used to represent the current user. In order to use this, the 'request' must have been provided as part of the context dictionary when instantiating the serializer.

owner = serializers.HiddenField(
    default=serializers.CurrentUserDefault()
)

More details here: http://www.django-rest-framework.org/api-guide/validators/#currentuserdefault

Hope this will help

Hisagr
  • 601
  • 6
  • 13
0

You would do something like the class

MySerializer(serializers.HyperlinkedModelSerializer):
    user = serializers.ReadOnlyField()
    class Meta:
        model = My
        fields = ('user','title','description')

When you call the serializer, you just add the current user into the serializer, either in views.py or in serializer create method.

  1. In views.py

    serializer.save(user=request.user)

  2. In serializer create method

    def create(self, validated_data): validated_data['user'] = request.user.id obj = ExampleModel.objects.create(**validated_data) return obj

shady
  • 455
  • 1
  • 7
  • 18
  • where exactly do I have to put def create(self, validated_data):? – sshussain270 Dec 23 '16 at 05:28
  • By syntax, your solution seems to be correct but i get the following error: is not JSON serializable – sshussain270 Dec 23 '16 at 05:34
  • oh, then you may want to change ` user = serializers.ReadOnlyField()` insto something like ` user = serializers.ReadOnlyField(source='user.id')` – shady Dec 23 '16 at 05:38
  • Thank you @shady but the problem still remains, how do I add request.user to the user field. Because source='user.id' adds null to it so i get error – sshussain270 Dec 23 '16 at 05:44
  • I already told you above, when you calling the serializer class save method in views.py, instead of using ExampleSerializer.save, you use ExampleSerializer.save(user=request.user). – shady Dec 23 '16 at 05:46
  • @Elisha512 If you got the error, you should remove the earlier added data records, since they are invalid, the user is null, which may cause the code to break. – shady Dec 23 '16 at 05:48