【发布时间】:2011-09-11 08:25:46
【问题描述】:
我想在 User 模型上注册一个信号处理程序,如下所示:
def post_save_handler(sender, instance, created, **kwargs):
should_have_profile = instance.has_perm('profile.should_have')
if should_have_profile:
profile, created = Profile.objects.get_or_create(user=instance)
if crated:
profile.save()
else:
old_profile = Profile.objects.filter(user=instance)
if old_profile:
old_profile.delete()
但是,在信号处理程序中,对新权限(通过更改组成员身份在视图代码中添加或删除)的“has_perm”测试没有正确进入。好像还没有应用新组。
我曾一度怀疑 contrib.auth.backends.py 中的 _group_perm_cache 和 _perm_cache,但我增强了我的信号处理程序以从传入实例中删除这些值,结果是相同的。
我只能推测,对当前组的任何更改都不会传递给该用户。为此,我还尝试在 User 对象上注册 m2m_changed 侦听器,但这也没有被调用(可能是因为 User.groups 没有实现为 ManyToManyField)。
有什么方法可以正确地做我想做的事吗?
【问题讨论】:
标签: django django-signals