Django QuerySet: Why can't I filter an annotated QuerySet?
I'm trying to retrieve a list of the 100 most popular books in my db, and then create a list of unique categories that are in that list. Here are my simplified Book, Favorite, and Category models:
class Book(models.Model):
name = models.CharField(max_length=100)
category = models.ForeignKey(Category)
class Favorite(mod开发者_高级运维els.Model):
user = models.ForeignKey(User)
book = models.ForeignKey(Book)
class Category(models.Model):
name = models.CharField(max_length=100)
I can easily get a list of the 100 most popular books using the following query:
books = Book.objects.annotate(num_favorites=Count('favorite')).order_by('-num_favorites')[:100]
But where I start having problems is when I try to get a list of unique categories for those 100 most popular books. The following query does not work (error posted below) and I can't seem to figure out why.
>>> categories = Category.objects.filter(book__in=books).distinct()
>>> categories
FieldError: Cannot resolve keyword 'num_favorites' into field. Choices are: category, favorite, id, name
Can anyone shed some light on what I'm missing here?
You might be piling too many things into one query. Try splitting it:
book_ids = (Book.objects.annotate(num_favorites=Count('favorite'))
.order_by('-num_favorites')[:100].values_list('id', flat=True))
categories = Category.objects.filter(book__in=book_ids).distinct()
精彩评论