Customize UserAdmin multiple times - python

I'd like to display custom models in the user admin backend from multiple apps.
I can do that for just one app with the classical admin.py commands :
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from my_user_profile_app.models import Employee
class EmployeeInline(admin.StackedInline):
model = Employee
can_delete = False
verbose_name_plural = 'employee'
# Define a new User admin
class UserAdmin(UserAdmin):
inlines = (EmployeeInline, )
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
However, the following app replaces all the previous registered models with its own, and so on…
Is there a clean way to append models in the user admin backend ?
It's like the app's admin.py must be aware of all previous customizations made, and register them all again.

I would suggest importing all your inlines into one of your app's admin.py, and defining the user admin there.
from myapp.admin import MyInline
from my_other_app.admin import MyOtherInline
# Define a new User admin
class MyUserAdmin(UserAdmin): # I would avoid using the same name
inlines = (MyInline, MyOtherInline)
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
Django doesn't have a mechanism to register additional inlines with a model admin automatically. You could probably create one, but the code might be fragile. I like the explicit approach above.

Related

Not all fields are showing correctly in django admin

I've added a plan field to my custom Account class, but cannot get it to show on the individual account page in the django admin. It shows correctly in table of all accounts in the list view (as denoted by list_display), but does not show on each individual account page.
Here's my Account model:
class Account(AbstractUser):
PLANS = (
("free", "free"),
("pro", "pro")
)
plan = models.CharField(max_length=10, choices=PLANS, default="free")
def __str__(self) -> str:
return self.first_name
And my admin file:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from accounts.models import Account
from subscribers.models import Subscriber
class SubscriberInline(admin.TabularInline):
model = Subscriber
extra = 0
class AccountAdmin(UserAdmin):
inlines = [SubscriberInline, ]
list_display = ("first_name", "plan", "email")
# fields = ("first_name", "plan", "email")
admin.site.register(Account, AccountAdmin)
Why does this happen?
Is the problem related to the custom Account class which inherits from the AbstractUser model? I thought to add fields to the AccountAdmin class, but that just returns the below error:
ERRORS:
<class 'accounts.admin.AccountAdmin'>: (admin.E005) Both 'fieldsets' and 'fields' are specified.
The plan field also doesn't show in the admin panel when trying to create a new account (mind you, neither do most of the other fields as it only asks for the username, password1 and password2 fields, and the option to add new subscribers to the table, but other fields like first_name etc can be edited in the admin after creation).
Thanks
UPDATE:
Adding user #bdbd's suggested changes seems to not make a difference to the admin area - am I adding this incorrectly? Here's my admin.py after adding #bdbd's changes:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from accounts.models import Account
from subscribers.models import Subscriber
from django.contrib.auth.forms import UserChangeForm
from django import forms
PLANS = (
("free", "free"),
("pro", "pro")
)
class MyAccountChangeForm(UserChangeForm):
plan = forms.ChoiceField(choices=PLANS)
class SubscriberInline(admin.TabularInline):
model = Subscriber
extra = 0
class AccountAdmin(UserAdmin):
form = MyAccountChangeForm
inlines = [SubscriberInline, ]
list_display = ("first_name", "plan", "email")
# #todo: bug! plan field not showing in django admin on individual account pages
admin.site.register(Account, AccountAdmin)
UPDATE 2:
Screenshots of admin area:
You should not extend from UserAdmin. Instead, you should create your own model admin class which extends from admin.ModelAdmin.
Then you should register your model separately.
class AccountAdmin(admin.ModelAdmin):
...
admin.site.register(Account, AccountAdmin)
As necessary, you can customize AccountAdmin to get the effect you want. You can peek at the UserAdmin source code to see how it is customized, if you want your admin view to behave similarly.
You need to override the default form that is being used by UserAdmin and add your field like so:
from django.contrib.auth.forms import UserChangeForm
from django import forms
class MyAccountChangeForm(UserChangeForm):
plan = forms.ChoiceField(choices=PLANS)
Then assign the form to your admin:
class AccountAdmin(UserAdmin):
form = MyAccountChangeForm

How to add some extra fields to the user in django-cms? (in django admin panel)

I would like to add some extra fields to user in django-cms (in django admin panel). How to do this in the simplest way?
Need to add two fields user bio and image. And can i use this in frontend to show a page with all the user info?
From the django docs
Create you custom user model like this
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User)
department = models.CharField(max_length=100)
Change the admin file like this
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from my_user_profile_app.models import Employee
# Define an inline admin descriptor for Employee model
# which acts a bit like a singleton
class EmployeeInline(admin.StackedInline):
model = Employee
can_delete = False
verbose_name_plural = 'employee'
# Define a new User admin
class UserAdmin(UserAdmin):
inlines = (EmployeeInline, )
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
OR
Another option is to define a custom user model. For more details please visit https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#a-full-example
Taken from Django documentation:
The simplest way would be to create what is often called a profile model. So for your example you would create something like
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User)
bio = models.TextField()
image = models.ImageField()
Then, for seeing this in the admin panel, you would reregister admin for the User
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from my_user_profile_app.models import Profile
class UserProfileInline(admin.StackedInline):
model = Profile
can_delete = False
verbose_name_plural = 'profile'
# Define a new User admin
class UserAdmin(UserAdmin):
inlines = (UserProfileInline, )
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
As far as showing this in frontend, you can use User as well as Profile the same way you would use other Django models.

Keep User and Group in same section in Django admin panel

I have made my own User model in Django 1.8 with
from django.contrib.auth.models import AbstractUser, Group, Permission
class MyUser(AbstractUser):
pass
But it does not figure in admin any longer.
I have tried adding it to the admin page with
from django.contrib.auth import get_user_model, models as auth_models
from django.contrib.auth.admin import UserAdmin
UserModel = get_user_model()
class MyUserAdmin(UserAdmin):
pass
admin.site.register(UserModel, MyUserAdmin)
and it does figure in admin now but in a different 'section' than Group. How can I keep User in the default section in the admin panel?
You need to add app_label to Meta class of MyUser.
class MyUser(AbstractUser):
pass
class Meta:
app_label = 'auth'
Official documentation on app_label.

How to move model to the other section in Django's site admin

Is it possible to move default Groups model from 'Authentication and Authoriation' section (on the Django admin site) to custom one and how to achieve that?
Let's start from the beginning in the other words.
I have a very simple application 'accounts' in my Django project.
models.py file looks like below:
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
def __str__(self):
return self.email
serializers.py file:
from rest_framework import serializers
from django.contrib.auth.models import Group
from django.contrib.auth import get_user_model
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
class UserSerializer(serializers.HyperlinkedModelSerializer):
groups = serializers.HyperlinkedRelatedField(
many=True,
required=False,
read_only=True,
view_name="group-detail"
)
class Meta:
model = get_user_model()
exclude = ('user_permissions',)
Now, on the admin site I have two sections: 'Accounts' and 'Authentication and Authorization'. 'Accounts' section contains my 'Users' table (for User model) and 'Authentication and Authorization' section contains 'Groups' table (for Django's default authorization Group model).
My question is - is it possible and how to move Groups table (model) to the 'Accounts' section?
I've even tried to create a custom 'Group' model based on Django's default auth Group model but have stuck on migration exceptions.
Is it possible to move default Groups model from 'Authentication and Authoriation' section (on the Django admin site) to custom one and how to achieve that?
Yes, it's possible.
1) You can move your model to section auth, just add to your class:
class Meta:
app_label = 'auth'
2) You can move Group and User models to your app section, for that variant need to:
Override user model and add it to your app
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
pass
also need add to your project settings AUTH_USER_MODEL = 'your_app.CustomUser'
Don't forget declare in admin.py from your app:
class UserAdmin(admin.ModelAdmin):
pass
admin.site.register(CustomUser, UserAdmin)
For group model put this code in admin.py:
from django.db.models.loading import get_models
from django.contrib.auth import models
models = get_models(models)
models[1]._meta.app_label = 'your_app'
3) You can look at django-admin-tools

Adding custom action to UserModel's Admin page

Is there any possibility to create custom action in admin page for django UserModel? I want automatize adding user to group (like adding him to staff, set some extra values, etc.), and of course create actions that take these changes back.
Thanks for your help.
Import User in your admin.py unregister it, create new ModelAdmin for it (or subclass the default one) and go wild.
It would look something like this I guess:
from django.contrib.auth.models import User
class UserAdmin(admin.ModelAdmin):
actions = ['some_action']
def some_action(self, request, queryset):
#do something ...
some_action.short_description = "blabla"
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Reference for actions.
Working example without losing all default inline actions etc.
Here we will add action which activates selected users.
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
def make_active(modeladmin, news, queryset):
queryset.update(is_active=True)
make_active.short_description = u"Activate selected Users"
class CustomUserAdmin(UserAdmin):
actions = [make_active]
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)

Categories