Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

499
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda