Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: m2m relationship is not getting updated in post_save signal
    primarykey
    data
    text
    <p>In my problem, the m2m relationship is not getting updated in post_save signal.</p> <p>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. </p> <pre><code>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) </code></pre> <p>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? </p> <p>Thank you ahead of time for your help!</p> <hr> <p>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: </p> <pre><code>LOG: statement: DELETE FROM "auth_user_groups" WHERE "auth_user_groups"."user_id" = 8 </code></pre> <p>A couple of statements later... </p> <pre><code>LOG: statement: INSERT INTO "auth_user_groups" ("user_id", "group_id") VALUES (8, 1) </code></pre> <p>So, it is properly being deleted, there is just something causing it to insert again?</p> <hr> <p>Update:</p> <p>Here is the repo/branch: <a href="https://github.com/jaycrossler/geoq-django/tree/guardian_setup/geoq" rel="nofollow">https://github.com/jaycrossler/geoq-django/tree/guardian_setup/geoq</a><br> Here is the specific location of the signal: <a href="https://github.com/jaycrossler/geoq-django/blob/guardian_setup/geoq/accounts/models.py" rel="nofollow">https://github.com/jaycrossler/geoq-django/blob/guardian_setup/geoq/accounts/models.py</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload