How do you override the admin model for Users? I thought this would work but it doesn't?
class UserAdmin(admin.ModelAdmin):
list_display = ('email', 'first_name', 'last_name')
list_filter = ('is_staff', 'is_superuser')
admin.site.register(User, UserAdmin)
I'm not looking to override the template, just change the displayed fields & ordering.
Solutions please?
You have to unregister User first:
class UserAdmin(admin.ModelAdmin):
list_display = ('email', 'first_name', 'last_name')
list_filter = ('is_staff', 'is_superuser')
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Maybe this question is also interesting for you: Customizing an Admin form in Django while also using autodiscover
Update tested with Django 3+
So you don't lose data like password encryption and the form itself, perform the import below
from django.contrib.auth.admin import UserAdmin
Define an AdminCustom class as the example and customize with the options you want, overriding the default.
class UserAdminCustom(UserAdmin):
list_display = ('email', 'first_name', 'last_name', 'is_staff', 'is_superuser')
list_filter = ('is_staff', 'is_superuser')
search_fields = ('username', )
And in the end, follow the example mentioned
admin.site.unregister(User)
admin.site.register(User, UserAdminCustom)
As pointed by #haifeng-zhang in the comments, it is useful to extend the default UserAdmin instead.
The official documentation about this can be found here:
https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
# Define a new User admin
class UserAdmin(BaseUserAdmin):
list_display = ('email', 'first_name', 'last_name')
list_filter = ('is_staff', 'is_superuser')
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Related
let me know how to remove the user_permission field from user and groups in a model as shown in below pic
You can remove a field from the default user admin by overriding the UserAdmin Class [source]
# admin.py
from django.contrib.auth.admin import UserAdmin
class CustomUserAdmin(UserAdmin):
# ...
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
(_('Permissions'), {
'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions'),
}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
admin.site.register(UserModel, CustomUserAdmin)
See detailed example
Use this to remove user_permission
exclude = ('user_permissions',)
Completing #SATYA's answer, you can do the following:
# admin.py
from django.contrib import admin
from django.contrib.auth.models import User
class CustomUserAdmin(admin.ModelAdmin):
exclude = ('user_permissions',)
admin.site.register(User, CustomUserAdmin)
You could also extend Django's UserAdmin (from django.contrib.auth.admin), but this could mess up if you use a custom User model.
On Django Admin, the default 'create user' form has 3 fields: username, password and confirm password.
I need to customize the create user form. I want to add the firstname and lastname fields, and autofill the username field with firstname.lastname.
How can I do this?
Something like this should work:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class UserCreateForm(UserCreationForm):
class Meta:
model = User
fields = ('username', 'first_name' , 'last_name', )
class UserAdmin(UserAdmin):
add_form = UserCreateForm
prepopulated_fields = {'username': ('first_name' , 'last_name', )}
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('first_name', 'last_name', 'username', 'password1', 'password2', ),
}),
)
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Only add_fieldsets is enough. No need to provide the add_form.
this would be the full code for admin.py, you can read about it in the Django docs here
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class UserAdmin(UserAdmin):
add_fieldsets = (
(
None,
{
'classes': ('wide',),
'fields': ('username', 'email', 'password1', 'password2'),
},
),
)
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
As a side note: 'classes':('wide',), sets the style of the field to open or "not collapsed", you can read more about the options for that here
When creating/adding a user with Django admin; how can this be customized?
For example, currently, the username and password are prompted for. However, I'd like to customize this to also have other required fields, such as 'email'?
The Django tutorial isn't clear on admin user auth customization?
Place this in your models.py.
from django.contrib.auth.models import User
User._meta.get_field('email').blank = False
Run makemigrations.
Run migrate.
See the documentation
Example for adding email to form when creating a user. In project admin.py added the following:
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
class UserAdmin(UserAdmin):
fieldsets = (
(None, {'fields': ('username', 'password','email')}),
(_('Personal info'), {'fields': ('first_name', 'last_name')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'password1', 'password2', 'email')}
),
)
form = UserChangeForm
add_form = UserCreationForm
try:
admin.site.unregister(User)
except NotRegistered:
pass
admin.site.register(User, UserAdmin)
This is how you can customize the user admin:
from django.contrib import admin
from django.contrib.auth import admin as upstream
# custom admin class for the User model, subclassing Django's one
class UserAdmin(upstream.UserAdmin):
add_form_template = ...
add_form = ...
add_fieldsets = ...
# unregister any existing admin for the User model and register mine
try:
admin.site.unregister(User)
except NotRegistered:
pass
admin.site.register(User, UserAdmin)
The parts I left with ... are the customizations you need to do to change the way the admin prompts for user creation.
Look for the original code in django/contrib/auth/admin.py and modify to taste.
I'm trying to create custom user models with their own admins (add and change forms). I'd like to have exactly the same as the original User admin with just the additional fields in the add and change forms.
Anybody could help?
models.py
from django.contrib.auth.models import User
class MyUser1(User):
#add more fields specific to MyUser1
class MyUser2(User):
#add more fields specific to MyUser2
admin.py
from django.contrib.auth.admin import UserAdmin
class MyUser1Admin(UserAdmin):
# how can I add the "fields specific to MyUser1" both in the add
# and the change forms?
admin.site.register(MyUser1, MyUser1Admin)
# same for MyUser2
I had to extend the UserCreationForm and UserChangeForm:
admin.py
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.admin import UserAdmin
from django.contrib import admin
from django import forms
class MyUser1CreationForm(UserCreationForm):
myuser1_field = forms.CharField()
class Meta:
model = User
fields = ("username", "myuser1_field", "password1", "password2")
class MyUser1ChangeForm(UserChangeForm):
myuser1_field = forms.CharField()
class Meta:
model = User
class MyUser1Admin(UserAdmin):
form = MyUser1ChangeForm
add_form = MyUser1CreationForm
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('myuser1_field', 'first_name', 'last_name', 'email')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'myuser1_field', 'password1', 'password2')}
),
)
admin.site.register(MyUser1, MyUser1Admin)
from django.contrib.auth.forms import UserChangeForm
class MyUser1AdminForm(UserChangeForm):
class Meta:
model = MyUser1
I wrote a custom admin class for Users in the Django Admin as follows:
class UserAdmin(admin.ModelAdmin):
model = User
list_display = ['email', 'first_name', 'last_name', 'last_login', 'date_joined', 'is_superuser', 'is_active']
list_filter = ['is_active', 'groups']
search_fields = ['email', 'first_name', 'last_name']
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
This breaks the 'change password' feature in the Django Admin. What do I have to add to my custom class to allow it to work again?
Thanks.
You need to inherit from django.contrib.auth.admin.UserAdmin, rather than
admin.ModelAdmin