How to Force Current User to a Field in Django Rest Framework #8220
-
Consider I have models, a serializer and a viewset as below: # models.py
class Foo(models.Model):
owner = models.ForeignKey(User, models.CASCADE, related_name="bars")
# serializers.py
class FooSerializer(serializers.ModelSerializer):
owner = serializers.PrimaryKeyRelatedField(
default=serializers.CurrentUserDefault(),
queryset=User.objects.all(),
)
class Meta:
fields = ["pk", "owner"]
# viewsets.py
# registered to /api/foos/
class FooViewSet(viewsets.ModelViewSet):
serializer_class = FooSerializer
permission_classes = [permissions.IsAuthenticated]
queryset = Foo.objects.all() So, when I send a POST request to However, I also want to totally ignore what the currently authenticated user sends as a value to I always want to have the current authenticated user as the value of the field How do I do that? Thanks in advance. Environment
|
Beta Was this translation helpful? Give feedback.
Asnwer