无法覆盖Django符号中的变量值

2024-05-16 19:13:02 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么当我在django信号中设置active_template = home.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)

Tags: testtruehome信号modelstemplateonedict