我想注册的用户模型的信号处理程序,它看起来是这样的:Django信号处理程序,监视组和权限的变化?
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实现)。
有什么办法可以正确地做我想要的吗?