为什么当我在django中设置active_template = home.template
时,一切看起来都很好,直到我再次“单击”管理面板中的“保存”按钮。然后将active_template变量再次设置为0。当我再次运行信号时,应该不会被覆盖吗?我尝试使用正常的功能,它可以正常工作,所以我想问题出在信号上。
型号:
class Homepage(models.Model):
choice_dict = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
TEMPLATE_TYPE = [
(choice_dict['one'], '1'),
(choice_dict['two'], '2'),
(choice_dict['three'], '3'),
(choice_dict['four'], '4'),
(choice_dict['five'], '5'),
]
template = models.IntegerField(choices=TEMPLATE_TYPE, null=True, blank=True)
content = RichTextUploadingField(null=True, blank=True)
信号
def default_value(sender, instance, **kwargs):
test = ['test','test1','test2']
home = Homepage.objects.first()
active_template = 0
if home.template and active_template != home.template:
Homepage.objects.filter(pk=2).update(content=test[home.template])
active_template = home.template
post_save.connect(default_value, sender=Homepage)