I have a many to many relationship between Group and User and also between User and Domain. Also i have one to many relationship between Domain and Group. I have defined the save_model function of UserAdmin as follows:
def save_model(self, request, obj, form, change):
form.save(False)
obj.save()
form.save_m2m()
for g in obj.group.all():
if g not in Group.objects.filter(domain__user=obj.id):
obj.group.remove(g)
obj.save()
print g.name
print obj.group.all()
The behavior that I was expecting is that when I remove a user from a domain, all the relationships between the user and the groups belonging to that domain will also be removed. However this is not happening. The last print statement correctly shows me the user-group relationships are removed. However when I query the database for some other page (or even from python prompt) I see that the relationships are not removed. Navigating to the user page and clicking save a second time fixes the issue. What am I missing here? Why is it not getting committed the first time? Thank you.