0

I'm playing with the book/author django documentation.

Is there a way to get books from author grouped by kinds in an array agg? (pgsql)

The following query is correct but I need information from authors table. Books.objects.values('kind').annotate(rating_min=Min('rating'),.. Etc.

Can I get author's books rating range grouped by kind ?

Author.objects.values('book__kind'). annotate(rating_min=Min('book__rating'))

Author.objects.annotate(rated_books=ArrayAgg('book'),...? 

Expecting something like

author.rated_books =[
    {'kind': 'fantasy', 'rating_min' : 2,
'rating_max' : 5, 'total' : 6},
]

Any idea to combine both?

vahdet
  • 6,357
  • 9
  • 51
  • 106
user2652620
  • 464
  • 2
  • 17
  • What do you need that for? What are you going to do with `rated_books`? – Endre Both Apr 02 '19 at 07:44
  • Is it uncommon query with orm? I just want to group ratings by kinds by author to understand how good an author is in several kinds (fantasy, romance,...) – user2652620 Apr 02 '19 at 08:28
  • What is uncommon is stuffing so much data into a single field. Depending on the models and what you want to do with the data, there is probably an easier way to do that than with a dictionary appended to `Author`. – Endre Both Apr 02 '19 at 08:49

0 Answers0