当前位置: 代码迷 >> python >> 用于表选择的 Django force_str 返回原始值
  详细解决方案

用于表选择的 Django force_str 返回原始值

热度:28   发布时间:2023-07-16 10:44:36.0

我有以下模型:

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' ,但我得到'RECEIVED' 我认为django.utils.encondig.force_str()可以完成这项工作,但似乎我误解了一些东西。

这确实是预期的行为。 _display method , so here, that should be:为了检索带有choices=…的字段的“值”,您使用,所以这里应该是:

class ServiceForListSerializer(CustomModelSerializer):

    # …

    def get_status(self, instance):
        status = instance.status.latest('timestamp').get_status_display()
        return force_str(status)