How to customize the auth.User Admin page in Django CRUD?

前端 未结 3 1414
走了就别回头了
走了就别回头了 2020-12-07 15:30

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

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 16:15

    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)
    

提交回复
热议问题