Admin User last_login as readonly

Pretty new to django. I am on 1.11 version.
I would like to modify the default User Admin form so that the last_login and date_joined fields are readonly for a user. How do you do that?

I’ve never had to do that before. Just browsing through the (very thorough!) admin page documentation, I found this a good place to start experimenting: https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields

-Jorge

I already reviewed the documentation. It shows how to make them readonly if your creating a new admin table/model. Not sure how to change the existing (default) User model.

Thanks for the suggestion though!

If you’re looking to change the Admin page for the User model, you’ll need to unregister the existing Admin form and register with your version:

admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)

From there, you can define your own CustomUserAdmin Form defining fields however you need them to be.

Ken

1 Like

Do I have to recreate all the fields already in the default User model? I really just want to use the default model, but add:
readonly_fields = ['last_login']

My guess would be no.

You could import the existing UserAdmin class, and make your CustomUserAdmin inherit from it - and then specify your readonly_fields for your CustomUserAdmin class.