【发布时间】:2011-03-12 00:03:29
【问题描述】:
基本上,我正在尝试使用来自一个模型的数据来触发另一个模型中的切换。
如果我的发票对象与一个文件相关联,我希望该文件被“锁定”(一个布尔值)。
我发现当我保存发票时,在将其与文件链接后,它不会记录 invoice_file.count() > 0 - 直到我下次打开发票并再次保存。请注意,我是在调用 super() 之后进行评估的,所以我觉得这充其量是令人困惑的。
class Invoice(models.Model):
...
invoice_file = models.ManyToManyField(UploadFile, null = True, blank = True)
def save(self, *args, **kwargs):
print('Invoice: saving!')
super(Invoice, self).save(*args, **kwargs)
print 'invoice_file count: %i' % self.invoice_file.count()
if self.invoice_file.count() > 0:
for invoice_file in self.invoice_file.all():
if(invoice_file.locked_status(1)) != 1: raise Exception('Couldn\'t set file locked status to 1 on file %s' % invoice_file.filename)
这会触发 UploadFile 模型中的一个函数:
class UploadFile(models.Model):
...
def locked_status(self, stat):
print('Locked status called.')
if stat == 1:
self.locked = True
self.save()
return 1
elif stat == 0:
self.locked = False
self.save()
return 0
def save(self, *args, **kwargs):
print 'UploadFile: Saving!'
super(UploadFile, self).save(*args, **kwargs)
【问题讨论】:
-
不确定如何具体解决您的问题,但尝试使用 Django 1.2 的 m2m 更改信号:docs.djangoproject.com/en/dev/ref/signals/#m2m-changed
-
谢谢斯派克!有趣的是,你应该提到信号,我刚刚删除了它们,因为它们使我的代码变得混乱,我发现重写 save() 和 delete() 更容易。然而,似乎信号提供了更好的控制程度,我可能只是重新启用它们,看看我是否能得到不同的结果。感谢您的建议!
标签: python django django-models many-to-many relationship