在我的问题中,m2m 关系没有在 post_save 信号中更新。
我有一个 post_save 用于检查以确保用户是工作人员,如果他们没有默认权限,则为他们分配默认权限。如果用户不是员工,则确认他们没有默认权限。
def user_post_save(sender, instance, **kwargs):
"""
If the user is staff and they don't have default auth permissions
assign them.
"""
group_ids = [g.id for g in instance.groups.all()]
if instance.is_staff and 1 not in group_ids:
# give them default auth permissions.
instance.groups.add(1)
elif not instance.is_staff and 1 in group_ids:
# if they are not staff and they have the permission, remove it.
instance.groups.remove(1)
post_save.connect(user_post_save, sender=User)
问题是 instance.groups 以正确的预期值到达 user_post_save 的末尾,但是它不会在数据库中更新。我错过了什么?
提前感谢您的帮助!
更多信息:我在玩,并从具有默认权限的用户那里拿走了员工身份。当我查看 postgres 日志时,我注意到以下内容:
LOG: statement: DELETE FROM "auth_user_groups" WHERE "auth_user_groups"."user_id" = 8
几句话后...
LOG: statement: INSERT INTO "auth_user_groups" ("user_id", "group_id") VALUES (8, 1)
那么,它被正确删除了,只是有什么东西导致它再次插入?
更新:
这里是repo/branch:https
://github.com/jaycrossler/geoq-django/tree/guardian_setup/geoq
这里是信号的具体位置:https ://github.com/jaycrossler/geoq-django/blob /guardian_setup/geoq/accounts/models.py