[Django]-Django admin – Auto save current user to model via inlines?

4👍

The example in the docs for save_formset shows how to do this. Save the formset with commit=False, then iterate through the instance and set the entered_by field to request.user.

class ProceedingAdmin(admin.ModelAdmin):
    ...
    def save_formset(self, request, form, formset, change):
        instances = formset.save(commit=False)
        for obj in formset.deleted_objects:
            obj.delete()
        for instance in instances:
            instance.entered_by = request.user
            instance.save()
        formset.save_m2m()

Note that your DocumentAdmin doesn’t need a save_formset method, since it doesn’t have any inlines.

Leave a comment