Django admin - how to save inlines?

robos85 picture robos85 · Aug 8, 2011 · Viewed 8.8k times · Source

I need to override save method of inlines in admin. While saving photos, I need to add user id to DB column. I cant make it in model because there is no request data there. How can I do it in admin, to somehow get nad set user id?

Answer

newmaniese picture newmaniese · Aug 9, 2011

I believe the save_formset method on ModelAdmin is what you should use:

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

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset