i want to set a field (current user) in a model automatically when an object gets created or is being updated and make it readonly, so the user can not change it:
In order to prepopulate the field:
@admin.register(Model)
class ModelAdmin(admin.ModelAdmin):
list_display = ('field_1', 'user')
def get_form(self, request, obj=None, **kwargs):
form = super(ModelAdmin, self).get_form(request, obj, **kwargs)
form.base_fields['user'].initial = request.user
return form
This works, but when i then want to make the field readonly using:
readonly_fields=('user', )
the app crashed with error:
KeyError at /admin/api/model/add/
'user'
Can anybody help me out and explain how i can reach my goal using django admin?
thanks!