Waqart
Waqart

Reputation: 113

How to add a date and time column in my Django default user page in admin

I am using the Django default user model and in my admin, I can see 5 columns which are username, email address, first name, last name, and staff status. I need to add another column here that displays the date joined. Can anyone help here? thanks in advance

User / admin.py

from django.contrib import admin
from .models import Profile
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin

class UserAdmin(AuthUserAdmin):
    
    list_display = ('username', 'date_joined', 'email')
    


admin.site.register(Profile)

Profile Model:

from django.contrib.auth.models import User


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default = 'default.jpg', upload_to = 'profile_pics')



    def __str__(self):
        return f'{self.user.username} Profile'

    def save(self):
        super().save()

Upvotes: 0

Views: 579

Answers (1)

iri
iri

Reputation: 744

Please refer to the documentation on this.

In your case you will want to alter your admin class to look something like:

from django.contrib.auth.admin import UserAdmin as AuthUserAdmin

@admin.register(User)
class UserAdmin(AuthUserAdmin):
    ...
    list_display = (..., 'date_joined', ...)
    ...

Upvotes: 2

Related Questions