Question

Je voudrais regrouper les enregistrements en deux catégories:

  1. Les produits ayant trois ou plusieurs enregistrements
  2. Les produits ayant moins de trois articles

Comment puis-je cela? Je cherche à utiliser annoter ().

Était-ce utile?

La solution

q = Book.objects.annotate(num_authors=Count('authors'))
books_with_3_or_over_authors = q.filter(num_authors__gte=3)
books_with_less_than_3_authors = q.filter(num_authors__lt=3)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top