信号仅在 shell 中有效,在管理和前端中无效

Signals working only in shell,not working in admin and frontend

我正在尝试在 post-save.The 模型

之后为模型字段分配一些值
class Authority(models.Model):
    no_of_feeds=models.PositiveIntegerField()
    no_of_rf=models.PositiveIntegerField()
    no_of_urf=models.PositiveIntegerField()


class Feed(models.Model):
        auth=models.ForeignKey(Authority,blank=False)

    @receiver(post_save, sender = Feed)
    def update_model_feed(sender, **kwargs):
        if kwargs['created']: #only fire when creating new objects
            kwargs['instance'].auth.no_of_feeds=kwargs['instance'].auth.feed_set.all().count()
            kwargs['instance'].auth.no_of_rf=kwargs['instance'].auth.feed_set.filter(resolved=True).count()
            kwargs['instance'].auth.no_of_urf=kwargs['instance'].auth.feed_set.filter(resolved=False).count()
            print '******************************'
        elif not kwargs['created']:
            kwargs['instance'].auth.no_of_feeds=kwargs['instance'].auth.feed_set.all().count()
            kwargs['instance'].auth.no_of_rf=kwargs['instance'].auth.feed_set.filter(resolved=True).count()
            print '-------------------------------'
            kwargs['instance'].auth.no_of_urf=kwargs['instance'].auth.feed_set.filter(resolved=False).count()

如您所见,信号用于在保存 Feed 后为 Authority 字段赋值 model.This 在 shell 中工作,因为字段会自动更新但它无法在管理员或前端创建或编辑提要实例 website.Though 它在 [=21= 执行时打印出 '------' 和 '****' ] 请帮忙

需要执行 kwargs['instance'].auth.save() 才能将对相关 Authority 的更改写入数据库。如果我不得不猜测,它看起来像是在 shell 中工作,因为您正在查看内存中未保存的对象,而在您的工作站点上,您正在查看从数据库返回的值。