0

the cause of the error is simple. The admin site asks me to select or browse user when I am logged in. So, I excluded the field to hide it in the admin form. It generates the integrity error.

How do I tell django to attach the currently logged in user (in the admin area) as the creator of the object?

I have seen a few posts that require me to make use of forms.py but I want to use the default admin template. Where do I make the edits at least?

Nie Selam
  • 1,343
  • 2
  • 24
  • 56

1 Answers1

1

Like that:

class ObjectAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.user = request.user
        obj.save()
Andrey Shipilov
  • 1,986
  • 12
  • 14
  • Note that this will change the user every time the object is updated in future. If you only want to set the user once when the object is created, then [check the value of change](http://stackoverflow.com/questions/31366720/django-admin-model-field-set-to-current-user/31366901#31366901). – Alasdair Apr 11 '17 at 08:43
  • @Alasdair true dat. – Andrey Shipilov Apr 11 '17 at 09:02
  • Thanks both. Works like a charm and Alasdaiir...thanks for the added tip. Helpful. – Nie Selam Apr 11 '17 at 09:59