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

215
Visualizações
Django Rest Framework Serializer charfield not updating when source is given

I have a model field with choices charfield

class Vehicle(models.Model):
    name = models.CharField(max_length=100)

    STATUS_CHOICES = (
        ("N", "New"),
        ("U", "Used"),
        ("P", "Just Purchased")
    )
    status = models.CharField(max_length=3, choices=STATUS_CHOICES)

The serializer class also has charfield for status but with source argument to display the readable value

class VehicleSerializer(ModelSerializer):
    status = serializers.CharField(source='get_status_display')

    class Meta:
        model = Vehicle

When I try to update vehicles through patch request with data {'status': "U"}, there is no update performed. However the update occurs when I remove the source from serializer status field. Providing source is necessary to display proper value in web view.

I know the option of changing the name of status in serializer to something other and using that in the template. Also there is the option to override update method in serializer, however my question is what is source doing to prevent the update?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I think you need to add status to the fields list in meta.

class VehicleSerializer(ModelSerializer):
     status = serializers.CharField(source='get_status_display')

     class Meta:
         model = Vehicle
         fields = ('status',)
about 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