[Answered ]-In Django how do I get code to run after an object is saved and after its many to many relationships are made?

2👍

Overriding Group.save(...) cannot work because the Group instance must be saved in the admin before the Membership instances are saved to ensure it exists in the database when the foreign keys are set on the memberships.

However, the Django admin provides you with a hook. You should override the save_related method in your ModelAdmin as described in the documentation:

[…] Here you can do any pre- or post-save operations for objects related to the parent. Note that at this point the parent object and its form have already been saved.

class GroupAdmin(admin.ModelAdmin):
    inlines = [PersonInlne]

    def save_related(self, request, form, formsets, change):
        super(GroupAdmin, self).save_related(request, form, formsets, change):
        group = form.instance
        # do sth. with group and its members

Leave a comment