In my problem, the m2m relationship is not getting updated in post_save signal.
I have a post_save that checks to make sure that the user is a staff member and if they don't have default permissions, then they are assigned default permissions. If the user is not staff, then it confirms that they don't have default permissions.
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)
The problem is that instance.groups makes it to the end of user_post_save with the correct intended values, however it doesn't update in the db. What I am missing?
Thank you ahead of time for your help!
More info: I was playing around and took away staff status from a user with default permissions. When I looked at the postgres logs, I noticed the following:
LOG: statement: DELETE FROM "auth_user_groups" WHERE "auth_user_groups"."user_id" = 8
A couple of statements later...
LOG: statement: INSERT INTO "auth_user_groups" ("user_id", "group_id") VALUES (8, 1)
So, it is properly being deleted, there is just something causing it to insert again?
Update:
Here is the repo/branch: https://github.com/jaycrossler/geoq-django/tree/guardian_setup/geoq
Here is the specific location of the signal: https://github.com/jaycrossler/geoq-django/blob/guardian_setup/geoq/accounts/models.py