Django自定义序列格式

时间:2017-07-29 04:09:01

标签: python django sequence

我想为采购订单创建自定义序列,例如PO-0001,为销售订单创建SO-0001等。

我尝试过这样的事情:

class Requisitions(models.Model):
    number = models.CharField(max_length=20, verbose_name='Number')
    transDate = models.DateField(verbose_name='Date')
    businessUnit = models.ForeignKey(BusinessUnit, verbose_name='Unit')
    division = models.ForeignKey(Division, verbose_name='Division')
    remarks = models.TextField
    status = models.IntegerField(verbose_name='Status')

    def save(self, *args, **kwargs):
        letter = 'REQ-'
        lastNumber = 1
        self.number = '%s%03d'(letter,lastNumber)

        super(self.__class__, self).save(*args, **kwargs)

我在保存新记录时在管理页面中出错:

'str' object is not callable
 Request Method:    POST
 Request URL:   http://localhost:8000/admin/procurement/requisitions/add/
 Django Version: 1.11.3
 Exception Type: TypeError
 Exception Value: 'str' object is not callable

如何解决该错误?

1 个答案:

答案 0 :(得分:0)

您省略了包含字符串格式(%)运算符。更改以下行:

self.number = '%s%03d'(letter,lastNumber)

self.number = '%s%03d' % (letter,lastNumber)

甚至更好,使用更现代的.format()函数:

self.number = '{0}{1:03d}'.format(letter, lastNumber)