문제

I have following models:

#models.py
class Blog(models.Model):
    author = models.ForeignKey(MyUser)

#author
class MyUser(AbstractUser):
    picture = models.ImageField()

I want to find out which authors have the most posts and get the top 6 in the most optimum way possible. I also want to get the picture from each author.

This is what I have, but I can't figure how to get the profile pic in the same query. Can anyone help please? Thanks,

authors = Blog.objects.values('author').annotate(posts=Count('author')).order_by('-posts')[:6]
도움이 되었습니까?

해결책

The bellow queryset should do the job:

authors = Blog.objects.values('author__picture').annotate(posts=Count('author')).order_by('-posts')[:6]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top