У меня есть следующая модель:
SERVICE_STATE_RECEIVED = 'RECEIVED'
SERVICE_STATE_WAITING_FOR_ASSESSMENT = 'WAITING_FOR_ASSESSMENT'
SERVICE_STATE_WARRANTY = 'WARRANTY'
SERVICE_STATE_QUOTED = 'QUOTED'
SERVICE_STATE_SCHEDULED = 'SCHEDULED'
SERVICE_STATE_REPAIRING = 'REPAIRING'
SERVICE_STATE_DOWN = 'DOWN'
SERVICE_STATE_FINISHED = 'FINISHED'
SERVICE_STATE_DELIVERED = 'DELIVERED'
SERVICE_STATE_CHOICES = (
(SERVICE_STATE_RECEIVED, _("Recibido")),
(SERVICE_STATE_WAITING_FOR_ASSESSMENT, _("Esperando valoración")),
(SERVICE_STATE_WARRANTY, _("En Garantía")),
(SERVICE_STATE_QUOTED, _("Cotizado")),
(SERVICE_STATE_SCHEDULED, _("Programado")),
(SERVICE_STATE_REPAIRING, _("En Reparación")),
(SERVICE_STATE_DOWN, _("Baja")),
(SERVICE_STATE_FINISHED, _("Servicio Concluido")),
(SERVICE_STATE_DELIVERED, _("Entregado")),
)
class ServiceStatus(CustomModel):
service = models.ForeignKey(Service, on_delete=models.PROTECT, related_name='status', verbose_name=_("Servicio"))
status = models.CharField(max_length=25, choices=SERVICE_STATE_CHOICES, verbose_name=_("Estatus"))
timestamp = models.DateTimeField(auto_now=True, verbose_name=_("Fecha y Hora"))
comment = models.TextField(null=True, blank=True, verbose_name=_("Comentarios"))
update = False
class Meta:
verbose_name = _("Estado del Servicio")
verbose_name_plural = _("Estados de los Servicios")
def __str__(self):
return "[{}] {}/{}/{}".format(self.id, self.service.id, self.status, self.timestamp)
И следующий сериализатор:
class ServiceForListSerializer(CustomModelSerializer):
product = serializers.SerializerMethodField()
serial_number = serializers.SerializerMethodField()
reception_date = serializers.SerializerMethodField()
status = serializers.SerializerMethodField()
class Meta:
model = models.Service
fields = ('id', 'product', 'serial_number', 'client_name', 'client_phone', 'comment', 'reception_date', 'status')
def get_product(self, instance):
product = instance.item.product.name
return product
def get_serial_number(self, instance):
serial_number = instance.item.serial_number
return serial_number
def get_reception_date(self, instance):
reception_date = instance.status.all().order_by('timestamp').first().timestamp
reception_date_to_return = reception_date.strftime("%d/%m/%Y")
return reception_date_to_return
def get_status(self, instance):
status = instance.status.all().order_by('timestamp').last().status
status_to_return = force_str(status)
return status_to_return
Я хочу, чтобы поле status
приносило подробное содержимое набора выборов, но я получить строковое значение в базе данных:
Я хочу, чтобы поле status
принесло 'Recibido' , но я получаю 'ПОЛУЧЕННЫЕ' . Я думал, что django.utils.encondig.force_str()
сделает эту работу, но, похоже, я что-то неправильно понял.