我在两个单独的模型函数中使用self.get_category_display()
:
class Document(models.Model):
category = models.IntegerField(choices=choices.DOC_CATEGORIES, null=True)
shipment = models.ForeignKey(Shipment, null=True)
file = models.FileField()
def __str__(self):
return "{}.{}.pdf".format(self.shipment.__str__(), self.get_category_display().upper())
def save(self, *args, **kwargs):
count = Document.objects.filter(shipment__purchase__contract=self.shipment.purchase.contract).count() + 1
new_name = "{}-{:03d}.{}.pdf".format(self.shipment.purchase.contract.number, count, self.get_category_display().upper())
self.file.name = new_name
super(Document, self).save(*args, **kwargs)
这在.__str__()
函数中可以正常工作,但在.save()
函数中却没有。对于.save()
,它只返回选项的整数而不是显示字符串。有没有办法从.save()
获取选择的字符串?