Home > database >  order_by date_join in Django
order_by date_join in Django

Time:05-21

I am displaying user information to the admin dashboard I want to order Users by their join date. and on another page, I want to display only 10 users information which are recently logged in

I am filtering user data like this

data = Profile.objects.filter(Q(user__is_superuser=False), Q(user__is_staff=False))

Model.py

class Profile(models.Model):
user = models.OneToOneField(User,default="1", on_delete=models.CASCADE)   
image = models.ImageField(upload_to="images",default="default/user.png")
def __str__(self):
   return f'{self.user} profile'

please help me to do this. Thanks in Advance

CodePudding user response:

I am assuming you have certain fields like last login in Profile to track login history

Use

user__last_login

Now you can filter as

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

CodePudding user response:

user.last_login

you can order user queryset based on last_login field and take first 10 using [:10] at end of queryset

  • Related