django 更新查询不起作用,但在 shell 中有效

django update query is not working but works in shell

我在 django 中有一个函数,它运行良好但查询集模型未更新

def SLATarget(instanceID):
    SLAstatus = 2
    print(SLAstatus)
    print(instanceID)
    ProcessInstance.objects.filter(no=instanceID).update(SLAstate=SLAstatus)

型号:

SLAoprators=[
  (0, 'in target'),
  (1, 'in milestone'),
  (2, 'Exceded milestone'),
  (3, 'Exceded target'),
  ]

class ProcessInstance(models.Model):
    no = models.CharField('NO.', max_length=100, blank=True)
    created_on = models.DateTimeField(auto_now_add=True)
    end_on = models.DateTimeField(null=True, blank=True)
    SLAstate = models.IntegerField(choices=SLAoprators,blank=True,null=True)

运行 在另一个 class 中并且函数不更新模型但是当在 shell 中调用时它工作

SLATarget("1221")

打印结果为

2

1221

我通过将模型格式从 integer 更改为 Charfield choicefield 来解决问题。 Django 版本是 3.1