当我尝试更新模型中的多个对象时,我发现了一些问题。这是我的模特:
class NumberSequence(models.Model):
code = models.CharField(max_length=12)
name = models.CharField(max_length=60)
prefix = models.CharField(max_length=3)
length = models.IntegerField()
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
l = '{0}-{1:0'+str(ns[0].length)+'d}'
for num in ns:
num.last = lastNumber
num.save()
return l.format(letter,lastNumber+1)
class Requisitions(models.Model):
number = models.CharField(max_length=20, default=getNumberSequence())
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')
当我在申请单中创建新记录时,表格序列不会更新。但如果我重新启动服务,数字序列表会自动更新。 我的代码发生了什么? 任何建议,请..
答案 0 :(得分:0)
您不应该在字段定义中调用默认函数,而是仅在没有括号的情况下传递callable。
number = models.CharField(max_length=20, default=getNumberSequence)