我可以在模型的save方法中使用get_FOO_display()吗?

时间:2018-09-17 17:40:46

标签: django

我在两个单独的模型函数中使用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()获取选择的字符串?

0 个答案:

没有答案