Display fields from one-to-one model - django

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

Related

How to add email in Django amin Add user Page

I have created a CustomUser(AbstractUser) Model and in this model I want to add email id field in admin Add User Page.Currently By default first we can enter username and Password and after creating username and password we are redirected to another page where email field is available I want this email field on add User page is this possible.?
On admin django already had a BaseUserAdmin which is the default, it have 2 parts, add form and change form which both forms created from fieldsets(change form) and add_fieldsets(add form)
To add fields or remove field on add form override the BaseUserAdmin add_fieldsets:
from django.contrib.auth.admin import UserAdmin
class CustomUserAdmin(UserAdmin):
add_fieldsets = UserAdmin.add_fieldsets + (
(None, {'fields': ('email',)}),
)
If you are using a custom ModelAdmin which is a subclass of
django.contrib.auth.admin.UserAdmin, then you need to add your custom
fields to fieldsets (for fields to be used in editing users) and to
add_fieldsets (for fields to be used when creating a user)
second method: override add_fieldsets attribute itself:
class CustomUserAdmin(UserAdmin):
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user.
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'email', 'password1', 'password2'),
}),
)
Document: https://docs.djangoproject.com/en/3.1/topics/auth/customizing/#custom-users-and-django-contrib-admin
In admin.py of your app, create a ModelAdmin and use the fields attribute to include the email field in the view.
Example:
from django.contrib import admin
from myproject.myapp.models import CustomUser
class CustomUserAdmin(admin.ModelAdmin):
fields = ('username', 'password', 'email')
admin.site.register(CustomUser,CustomUserAdmin)

Adding "group" field in Django User Admin

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

fields except the id field is not getting imported using Django import export

These are my codes and csv file
class UserResource(ModelResource):
class Meta:
model = User
fields = ('username', 'email', 'is_member')
import_id_fields = ('username',)
class UserAdmin(ImportMixin, admin.ModelAdmin):
resource_class = UserResource
admin.site.register(User, UserAdmin)
username, email, is_member, id
abc, abc#mail.com, True,
When you set:
fields = ('username', 'email', 'is_member')
you are restricting the fields you want to show from all to only the ones you declare.
The above statement means:
import only these fields I'm telling you to import
But if you do:
fields = ('username', 'email', 'is_member', 'id')
I'm guessing you're going to get what you need

Django - Custom admin page not displaying properties

In my project, I use the AbstractUser class to override the default User model so I can add extra properties. I want these properties to be added to the admin page, therefore I can view them. The code below is how I have customized my admin page so far.
forms.py:
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = User
fields = ('username', 'email', 'following', 'posts')
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = User
fields = ('username', 'email', 'following', 'posts', )
admin.py:
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = User
list_display = ['username', 'email', 'bio', 'profilePic',]
admin.site.register(User, CustomUserAdmin)
When I click on a user object on the admin page, I can view all the properties such as a users username and email, however I want to view properties such as following and posts, which are both ManyToManyFields.
I supply these properties in the CustomUserCreationForm and the CustomUserChangeForm, however I can't still view these properties.
Does anybody know how I can view these fields when I view or create a new user object? Thank you.

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.