Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 638
Natim Avatar asked Feb 16 '10 03:02

Natim


People also ask

How do I give permission to admin in Django?

Test the 'view' permission is added to all modelsUsing #3 for Django 1.7 only creates the permission objects if the model doesn't already exist. Is there a way to create a migration (or something else) to create the permission objects for existing models?


2 Answers

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) 
like image 89
Natim Avatar answered Sep 21 '22 18:09

Natim


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) 
like image 31
Fernando Freitas Alves Avatar answered Sep 20 '22 18:09

Fernando Freitas Alves