错误更新格式化序列号表

时间:2017-07-31 03:04:33

标签: django numbers sequence models

我有这样的代码:

class NumberSequence(models.Model):
    code = models.CharField(max_length=12)
    prefix = models.CharField(max_length=3, verbose_name='Prefix')
    length = models.IntegerField(verbose_name='Digit Length')
    last = models.IntegerField(verbose_name='Last Number Used')

def getNumberSequence():
    ns = NumberSequence.objects.filter(code='REQ')
    letter =  ns[0].prefix
    lastNumber = ns[0].last+1
    formatedNS = '{0}-{1:0'+str(ns[0].length)+'d}'
    NumberSequence.objects.filter(code='REQ').update(last=lastNumber)
    return formatedNS.format(letter,lastNumber)

class Requisitions(models.Model):
    number = models.CharField(max_length=20, default=getNumberSequence)

但是当我在申请单中创建新记录时,NumberSequence中的最后一个数字更新为lastNumber + 2.

示例:last = 1.当我创建新记录时,最后更新为3.最后一个应更新为2.

我的代码出了什么问题?感谢

0 个答案:

没有答案