[Django]-DRF, add custom field to ModelSerializer

5👍

try to use SerializerMethodField field as

class TaskSerializer(serializers.ModelSerializer):
    is_in_usertask = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Task
        fields = ('name', 'is_in_usertask')

    def get_is_in_usertask(self, task):
        return UserTask.objects.filter(user=self.context['request'].user, goal=task.goal).exists()
👤JPG

0👍

Take a look to this conversation: How to get Request.User in Django-Rest-Framework serializer?

You can’t access to request.user directly

Leave a comment