0

I want to display all users information on admin dashboard with their last_login and join date I filter user date to display on template

    data = Profile.objects.filter(Q(user__is_superuser=False), Q(user__is_staff=False)).order_by('-user__last_login')[:10]

Profile model

 user = models.OneToOneField(User,default="1", on_delete=models.CASCADE, 
related_name="profile")
image = models.ImageField(upload_to="images",default="default/user.png")

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

I want to do like this

In the last_login and date_joined column, I want to print all users last login date time and join date time

Ajay
  • 31
  • 1
  • 6

1 Answers1

1

try this in admin.py of your app for User Admin

from django.contrib.auth.admin import UserAdmin


class MyUserAdmin(UserAdmin):
    list_display = UserAdmin.list_display + ("last_login","date_joined")

However, if you want to show this information in your Profile Admin you should use this code

from django.contrib import admin

class ProfileAdmin(admin.ModelAdmin):
    list_display =  ("last_login","date_joined")
    
    def last_login(self, instance):
        return instance.user.last_login
    
    def date_joined(self, instance):
        return instance.user.date_joined
    
armin NaCl
  • 11
  • 2
  • Thanks Armin NaCl for you response, I am using superuser as admin in my project is this solution works for that condition – Ajay May 22 '22 at 19:49
  • how can I display it on template – Ajay May 23 '22 at 03:44
  • Please provide full solution – Ajay May 23 '22 at 03:56
  • yes it works for superusers and you should just open the URL of your Django admin dashboard, for displaying the images field in admin you can Reference this link (https://stackoverflow.com/a/16307554/19175189) – armin NaCl May 23 '22 at 05:12