У меня есть следующие модели:
class Service(CustomModel):
item = models.ForeignKey('inventory.ProductItem', on_delete=models.PROTECT, related_name='services', verbose_name=_("Artículo"))
reference = models.ForeignKey('operations.Sale', on_delete=models.PROTECT, related_name='services', verbose_name=_("Referencia"))
client_name = models.CharField(max_length=100, verbose_name=_("Nombre del Cliente"))
client_phone = models.CharField(max_length=10, verbose_name=_("Teléfono del Cliente"))
comment = models.TextField(verbose_name=_("Observaciones"))
class Meta:
verbose_name = _("Servicio")
verbose_name_plural = _("Servicios")
def __str__(self):
return "[{}] {}/{}/{}".format(self.id, self.client_name, self.item.serial_number, self.item.product.name)
def save(self, *args, **kwargs):
new = False if self.id else True
super().save(*args, **kwargs)
if new:
initial_status = ServiceStatus()
initial_status.service_id = self.id
initial_status.status = SERVICE_STATE_RECEIVED,
initial_status.save()
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, _("En Evaluació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)
Как вы можете видеть, когда создается экземпляр Service
, он автоматически создает экземпляр ServiceStatus
, но поле ServiceStatus.status
получает wefgdsird значение:
Мне кажется, Django не удается управлять кодированием, но я не могу найти то, что делаю неправильно.