I just want to add the subscription date in the User list in the Django CRUD Administration site. How can I do that ?
Thank you for your help
I just want to add the subscription date in the User list in the Django CRUD Administration site. How can I do that ?
Thank you for your help
I finally did like this in my admin.py file :
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
UserAdmin.list_display = ('email', 'first_name', 'last_name', 'is_active', 'date_joined', 'is_staff')
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
admin.py
file at the root of your Django project (on the same level as your wsgi.py or settings.py files.) –
Otology from django.contrib import admin
? –
Otology Another way to do this is extending the UserAdmin class.
You can also create a function to put on list_display
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class CustomUserAdmin(UserAdmin):
def __init__(self, *args, **kwargs):
super(UserAdmin,self).__init__(*args, **kwargs)
UserAdmin.list_display = list(UserAdmin.list_display) + ['date_joined', 'some_function']
# Function to count objects of each user from another Model (where user is FK)
def some_function(self, obj):
return obj.another_model_set.count()
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
__init__
), or is there a problem with it? class CustomUserAdmin(UserAdmin): list_display = list(UserAdmin.list_display) + ['date_joined']
–
Donyadoodad CustomUserAdmin.list_display =
.. rather than UserAdmin.list_display =
? –
Reynold CustomUserAdmin.list_display =
is the cleaner solution –
Expertise In admin.py
Import UserAdmin
from django.contrib.auth.admin import UserAdmin
Put which fields you need:
UserAdmin.list_display = ('email','is_active') # Put what you need
Thats all! It works with Django3
list_display
with additional fields (using Django4): UserAdmin.list_display = list(UserAdmin.list_display) + ['is_active', 'date_joined']
–
Impatient Assuming that your user class is User
and your subscription date field is subscription_date
, this is what you need to add on your admin.py
class UserAdmin(admin.ModelAdmin):
list_display = ('subscription_date',)
admin.site.register(User, UserAdmin)
© 2022 - 2024 — McMap. All rights reserved.