私の問題では、post_save シグナルで m2m 関係が更新されません。
ユーザーがスタッフであることを確認する 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)
それで、それは適切に削除されていますが、それを再び挿入する原因は何ですか?
アップデート:
リポジトリ/ブランチは次のとおりです: https://github.com/jaycrossler/geoq-django/tree/guardian_setup/geoq
シグナルの特定の場所は次のとおりです: https://github.com/jaycrossler/geoq-django/blob /guardian_setup/geoq/accounts/models.py