16

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

Community
  • 1
  • 1
FurtiveFelon
  • 14,714
  • 27
  • 76
  • 97

1 Answers1

30

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()
Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148
  • 1
    Thanks a lot! Just to provide some more substantial example: http://stackoverflow.com/questions/1477319/how-to-get-the-django-current-login-user – FurtiveFelon Jun 15 '10 at 19:56
  • Are you sure: if not change: is correct? Shouldn't it be if change:? Thanks a bunch for your example!!! Up voted. – Luke Dupin May 03 '16 at 04:33
  • 2
    @LukeDupin I think this is correct. As written it would only set the user when the inline object is created but not on updates. – TehCorwiz May 24 '16 at 14:57
  • 1
    https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset – Wtower Mar 10 '17 at 15:56