I have Comment model relationship with User
Like this Model
class Comment(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
comment = models.CharField()
type = models.CharField()
point = models.CharField()
Serializer
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Card
fields = ['comment', 'type', 'point']
From here I already get the API response, to save in the comment model
def save_response_data(data):
# here validate...
json_data = {}
# I want the user instance that is making the request
serializer = CommentSerializer(data=json_data)
if serializer.is_valid():
serializer.save()
return json_data
views request api
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
...
...
def post(self, request):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data, status=...)
# Overwrite create method
def create(self, request, *args, **kwargs):
super(UserViewSet, self).create(request, args, kwargs)
# Currently, this is how I am obtaining the user who makes the request
user_id = request.user.id
# Simple queryset build payload to send api
payload = build_payload(user_id)
response = requests.post(url, data=payload)
data = response.json()
save_response_data(data)
I want the user instance that is making the request
I tried to do it with signals but it doesn't work, any idea or comment, it would be helpful