django post_save信号更新 [英] django post_save signals on update

查看:154
本文介绍了django post_save信号更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一些类似于以下的post_save接收器

I am trying to set up some post_save receivers similar to the following

@receiver(post_save, sender=Game, dispatch_uid='game_updated')
def game_updated(sender, **kwargs):

    '''DO SOME STUFF HERE'''

    MyPick.objects.filter(week=game.week, team=game.home_team).update(result=home_result)
    MyPick.objects.filter(week=game.week, team=game.away_team).update(result=away_result)


@receiver(post_save, sender=MyPick, dispatch_uid='user_pick_updated')
def update_standings(sender, **kwargs):
    '''DO STUFF'''

第一个接收器在Game对象更新后正确调用,但是调用MyPick对象上的更新不会导致第二个接收器被调用。 post_save信号不能在更新中工作,或者我在这里还缺少其他东西?

The first receiver is getting called correctly after an update on the Game object, however the calls to update on the MyPick object are not causing the second receiver to be called. Does the post_save signal not work on update or am I missing something else here?

谢谢

推荐答案

update()直接转换为SQL语句;它不会在模型实例上调用 save(),因此 pre_save post_save 信号不发出。如果您希望您的信号接收器被调用,您应该循环查询查询器,并为每个模型实例进行更改,并自行调用 save()

update() is converted directly to an SQL statement; it doesn't call save() on the model instances, and so the pre_save and post_save signals aren't emitted. If you want your signal receivers to be called, you should loop over the queryset, and for each model instance, make your changes and call save() yourself.

这篇关于django post_save信号更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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