Posts

Showing posts with the label Membership

Assign User-objects To A Group While Editing Group-object In Django Admin

Answer : The save method above won't work if you add a new group and simultaneously add users to the group. The problem is that the new group won't get saved (the admin uses commit=False) and won't have a primary key. Since the purpose of save_m2m() is to allow the calling view to handle saving m2m objects, I made a save object that wraps the old save_m2m method in a new method. def save(self, commit=True): group = super(GroupAdminForm, self).save(commit=commit) if commit: group.user_set = self.cleaned_data['users'] else: old_save_m2m = self.save_m2m def new_save_m2m(): old_save_m2m() group.user_set = self.cleaned_data['users'] self.save_m2m = new_save_m2m return group yourapp/admin.py from django import forms from django.contrib import admin from django.utils.translation import ugettext_lazy as _ from django.contrib.admin.widgets import FilteredSelectMultiple from django.contr...