Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda