Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

500
Views
How to Force Current User to a Field in Django Rest Framework

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 /api/foos/ with an empty body, it creates a Foo instance with its owner field set to the current logged-in user, which is fine.

However, I also want to totally ignore what the currently authenticated user sends as a value to owner. For example, if I do a POST request to /api/foos/ with body user=5 (another user, basically), CurrentUserDefault sets the Foo.owner to that user, which is not the behavior I want to have.

I always want to have the current authenticated user as the value of the field owner of a new Foo instance, or, in other words, I want FooSerializer.owner field to be set as currently authenticated user as a value and ignore what is sent on POST request as a value of owner.

How do I do that?

Thanks in advance.


Environment

  • django ^2.2
  • djangorestframework ^3.12.4
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Since you don't want owner to be modifiable via your serializer, I'd suggest you to remove the field from the serializer or make it read-only.

You can then set the owner using the serializer's save method that allows you to inject additional data.

A good place for this in your example would be the perform_create method of your ModelViewSet. For example:

class FooViewSet(viewsets.ModelViewSet):
    serializer_class = FooSerializer
    permission_classes = [permissions.IsAuthenticated]
    queryset = Foo.objects.all()

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!