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
- [Django]-DRF APIView – How can I add help text to auto generated form?
- [Django]-When running Celery with Django's manage.py command, it returns a strange error
- [Django]-Python range() builtin function, erm… malfunctioning with Django
- [Django]-Django Global function and variable
Source:stackexchange.com