Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why save_model method doesn't work in admin.StackedInline?

I have a similar problem as a previously solved problem of mine, except this time solution doesn't seem to work:

How to auto insert the current user when creating an object in django admin?

Previously i used to override the save_model to stamp the user submitting the article. Now i need to do the same for comments, it doesn't seem to work anymore.

Anyone have any ideas?

Thanks a lot!

Jason

like image 430
FurtiveFelon Avatar asked Jun 15 '10 19:06

FurtiveFelon


1 Answers

The saving isn't done in the InlineAdmin's save_form, you have to look at save_formsets in the ModelAdmin to which the inlines belong:

 class MyAdmin(admin.ModelAdmin):
    inlines = [MyInlineAdmin,.....]

    def save_formset(self, request, form, formset, change):
        formset.save()
        if not change:
            for f in formset.forms:
                obj = f.instance 
                obj.user = request.user
                obj.save()
like image 81
Bernhard Vallant Avatar answered Sep 23 '22 20:09

Bernhard Vallant