我想註冊的用戶模型的信號處理程序,它看起來是這樣的: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實現)。
有什麼辦法可以正確地做我想要的嗎?