信号只在shell中工作,不在管理和前端工作 [英] Signals working only in shell,not working in admin and frontend

查看:208
本文介绍了信号只在shell中工作,不在管理和前端工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在保存后为模型字段分配一些值。模型是

I am trying to assign some values to a model field after post-save.The model is

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 model.This在shell中工作,因为这些字段会自动更新,但它无法在管理员中创建或编辑Feed实例或前端网站。尽管它在控制台中打印出-------和****.Pls有帮助

As you can see,The signal is used to assign values to Authority fields after saving Feed model.This works in shell,as the fields are automatically updated but it doesnt work on creating or editing a feed instance in admin or front-end website.Though its print out the '-------' and '****' on execution in the console.Pls kindly help

推荐答案

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

Need to do kwargs['instance'].auth.save() in order to write those changes to the related Authority to the database. If I had to guess, it looks like it's working in the shell because you're looking at the unsaved object in memory, while on your working site you're looking at the values returned from the database.

这篇关于信号只在shell中工作,不在管理和前端工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆