Adding "group" field in Django User Admin - django

I am trying to add a "group" field to show group permissions on the user panel in Django 3.1.2. I tried combining this and this, but always end up in either The model User is already registered with 'auth.UserAdmin' or The model User is not registered (when trying to unregister first):
from django.contrib import admin
from django.contrib.auth.models import User
# fails in "model User not registered"
admin.site.unregister(User)
#fails in already registered with auth.UserAdmin
#admin.register(User)
class UserAdmin(admin.ModelAdmin):
def group(self, user):
return ' '.join([g.name for g in user.groups.all()])
list_display = ['username', 'email', 'first_name', 'last_name', 'is_active', group]
list_filter = ['groups', 'is_staff', 'is_superuser', 'is_active']
How would I correctly register my custom UserAdmin?

I don't remeber, where I saw answer on SO, but do like:
admin.site.unregister(User) before.
Than modificate admin.model, as you wish, and then, register again.
from django.contrib.auth.models import User, Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
admin.site.unregister(User)
class GroupInline(admin.StackedInline):
model = Group
#admin.register(User)
class UserAdmin(BaseUserAdmin):
inlines = (GroupInline,)

If you want to show the group a user belong to in the list display:
#admin.register(User)
class UserAdmin(auth_admin.UserAdmin):
def group(self, user):
groups = []
for group in user.groups.all():
groups.append(group.name)
return ' '.join(groups)
group.short_description = 'Group'
and now in your list_display you can have it as this:
list_display = ['username', 'email', 'first_name', 'last_name', 'is_active', 'group']
I hope this works for you

Related

How can I display the column ID in the django-admin users table?

How can I show besides the columns username, email, first name, last name, staff user a 5th column with the users ID. I think I have to write some code into admin.py like:
class CustomUserAdmin(UserAdmin):
readonly_fields = ('id',)
admin.site.register(CustomUserAdmin)
You should take advantage of ModelAdmin.list_display, docs here --> https://docs.djangoproject.com/en/2.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display
class CustomUserAdmin(UserAdmin):
list_display = ('username', 'email', 'first_name', 'last_name', 'id')
readonly_fields = ('id',)
admin.site.register(User, CustomUserAdmin)
In case you incur in an AlreadyRegistered error, you should add the following prior to your admin.site.register line
admin.site.unregister(User)
class UserAdmin(admin.ModelAdmin):
list_display = ['first_name', 'last_name',....., 'user_id']
class Meta:
model = User
admin.site.register(User, UserAdmin)
Add user_id field in your list display it will show.
A little late to the party but I would like to contribute.
When I tiried the accepted solution, I realized that some functionality is lost with the User admin page (Maybe other functionalities added later on). Here is the solution for whom do not want to loose other functionalities:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
admin.site.unregister(User)
# Register your models here.
#admin.register(User)
class CustomUserAdmin(UserAdmin):
readonly_fields = ("id",)
def __init__(self, model, admin_site):
super().__init__(model, admin_site)
self.list_display = ("id",) + self.list_display
I used the default UserAdmin class and inherited from it. Than adding "id" field to the super's list_display field solved it elegantly.
Note that solution can be improved by using AUTH_USER_MODEL if you are using custom user class.

How do I add or delete attributes on the 127.0.0.1:8000/admin/auth/user page?

How do I add or delete attributes on the 127.0.0.1:8000/admin/auth/user page?
I don't know how to add or remove properties from an existing page
class CustomAdmin(admin.ModelAdmin):
list_display = ['emp_no', 'first_name', 'last_name', 'gender', 'birth_date', 'hire_date']
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Nothing changes
try this code to changes the django admin auth user fields display
from django.contrib.auth.admin import UserAdmin
from django.contrib import admin
class CustomAdmin(UserAdmin):
list_display = ['emp_no', 'first_name', 'last_name', 'gender', 'birth_date', 'hire_date']
admin.site.register(User, CustomAdmin)
I advise you to change the admin page name (to be more specific) and unregister group if you don't need it (yet) :
from django.contrib.auth.admin import UserAdmin
from django.contrib import admin
admin.site.site_header = 'Your administration name'
class CustomAdmin(UserAdmin):
list_display = ['emp_no', 'first_name', 'last_name', 'gender', 'birth_date', 'hire_date']
admin.site.register(User, CustomAdmin)
admin.site.unregister(Group)

Incorrect work redefined form in django admins

I've created custom model Profile and linked it to the User model which works fine. But, now I want to create custom UserCreateForm in Django admin. I redefined it and added necessary fields, but after that still shows, all fields from profile model, ex: phone, home_address. I need fields displayed as : 'first_name', 'last_name', 'username', 'password1', 'password2' in the UserCreateForm. What have I done wrong?
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm
from .models import Profile
class NewUserCreateForm(UserCreationForm):
class Meta:
fields = ('username', 'first_name', 'last_name',)
class ProfileInline(admin.TabularInline):
model = Profile
class UserAdmin(UserAdmin):
add_form = NewUserCreateForm
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('first_name', 'last_name', 'username','password1', 'password2', ),
}),
)
inlines = [ProfileInline]
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Firstly your models.py file will be :
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Profile(models.Model):
user_id = models.OneToOneField(User, on_delete=models.CASCADE)
phone = models.CharField(max_length=100)
address = models.CharField(max_length=256)
...
Then your admin.py file.
from.models import Profile
# Register your models here.
class ProfileAdmin(admin.ModelAdmin):
model = Profile
exclude = ['phone',] # Or whatever you don't want to display.
admin.site.register(Profile, ProfileAdmin)
For extended implementation please add or remove based on your needs.
Hope it helps.

Display fields from one-to-one model

I have a User model (stock django), linked with a UserProfile via a OneToOneField. Is it possible to display data from the UserProfile in the admin, when displaying users in tabular view?
I am doing:
class UserAdmin(UserAdmin):
inlines = (UserprofileInline,)
list_display = (
'username', 'email', 'first_name', 'last_name', 'is_staff'
)
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
How can I extend the list_display parameter to specify fields belonging to the UserProfile?
For example if you have an address field in UserProfile model then you can do like this
class UserAdmin(UserAdmin):
inlines = (UserprofileInline,)
list_display = (
'username', 'email', 'first_name', 'last_name', 'is_staff','address'
)
def address(self,obj):
return obj.userprofile.address
You can register your extended model 'UserProfile' to the admin site.
In that model, you can show your fields from OneToOneField User model.
Please check this out:
One to One Field Django Admin

Django 1.5 custom User model error. "Manager isn't available; User has been swapped"

I extend the django user model as described in the dev doc. I wan't to keep most of the original User model features so I extend the AbstractUser class. I've defined in settings.py:
AUTH_USER_MODEL = 'myapp.CustomUser'
My user class:
class CustomUser(AbstractUser):
custom_field = models.ForeignKey('OtherModel')
objects = UserManager()
Everything seems to work fine but when I try to make it managed by the admin site:
admin.site.register(CustomUser, UserAdmin)
I get this error on the admin CustomUser creation page (after validation of the password confirmation form):
AttributeError: Manager isn't available; User has been swapped for 'myapp.CustomUser'
The point is that I need this model managed by the admin site in order to have the same creation process as with the original User model (two step process with password validation).
You need only change form for adding user(overwrite clean_username and change User on get_user_model()
Full working example(If you inherited from AbstractUser)
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
class MyUserChangeForm(UserChangeForm):
class Meta:
model = get_user_model()
class MyUserCreationForm(UserCreationForm):
class Meta:
model = get_user_model()
def clean_username(self):
username = self.cleaned_data["username"]
try:
get_user_model().objects.get(username=username)
except get_user_model().DoesNotExist:
return username
raise forms.ValidationError(self.error_messages['duplicate_username'])
class MyUserAdmin(UserAdmin):
form = MyUserChangeForm
add_form = MyUserCreationForm
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(MyUser, MyUserAdmin)
I struggled with this error for hours. For me I needed to remove all references to
from django.contrib.auth.models import User
and then replace it with:
from myapp.models import MyUser as User
This is assuming your custom User model is in an app called myapp and you called the model MyUser.
I'm using as User so that I don't have to change where my existing code that makes reference to the the User object from django.contrib.auth.models.
Good luck!
Alan
#aviars
You probably should look at full example in official documentation:
https://docs.djangoproject.com/en/dev/topics/auth/customizing/#a-full-example
There are some uncovered questions (permissions handling, for example), but, at least, it's working.
From Django docs:
You should also define a custom manager for your User model.
Subclass AbstractUser already handle objects = UserManager() for you (this code on github at line 327). You don't need to define it in your model again.
I'm not sure why it come with that error. But below config seem work for me with latest Dev version.
Model.py:
class CustomUser(AbstractUser):
custom_field = models.ForeignKey('OtherModel')
# remove : objects = UserManager()
I've found a solution:
I don't use UserAdmin to register the CustomUser in the admin site, I use a custom ModelAdmin.
class CustomUserAdmin(admin.ModelAdmin):
add_form = CustomUserCreationAdminForm
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'password1', 'password2')}
),
)
def get_fieldsets(self, request, obj=None):
if not obj:
return self.add_fieldsets
return super(CustomUserAdmin, self).get_fieldsets(request, obj)
def get_form(self, request, obj=None, **kwargs):
defaults = {}
if obj is None:
defaults.update({
'form': self.add_form,
'fields': admin.util.flatten_fieldsets(self.add_fieldsets),
})
defaults.update(kwargs)
return super(CustomUserAdmin, self).get_form(request, obj, **defaults)
Because I want to have a creation form different from the update form, I override the get_form function of the Model Admin as done in the UserAdmin django code. I also have created a custom Creation form for my custom user: CustomUserCreationForm
I received a response in the django-users mailing list that may be better than mine:
Unregister the original User class from the admin site and then register the CustomUser with UserAdmin:
admin.site.unregister(User)
admin.site.register(CustomUser, UserAdmin)
Not tested yet.
EDIT: this last solution doesn't work
Regards
UserAdmin is already registered to manage User, at the end of contrib/auth/admin.py
admin.site.register(User, UserAdmin)
Given you want to register UserAdmin with CustomUser, I think you would first have to unregister the User/UserAdmin pair:
admin.site.unregister(User)
then register your new model. Given the lack of control in the order of module loading, you could create a class derived from UserAdmin and manage your user model with it. This would work everytime, if I'm thinking properly.