Django retrieve get_foo_value within values_list and annotate query

Adharsh Manikandan :

I Have Written A Query that returns (rating, count)

result = list(Film.objects.values_list('rating').annotate(count=Count('rating')))

rating is a choice field of

rating = (
        ("5","Excellent"),
        ("4","Good"),
        ("3","Average"),
        ("2","Bad"),
        ("1","Very Bad"),
    )

I currently get the result like this

[(5, 10), (4,8), (3,7), (2,1), (1,4)]

I wish to get the pairs like this

[("Excellent",10),("Good",8)...]

Is there anything I can do within the Query to get it ? Whats the best method to get the desired result ?

Something like get_foo_display within this one.

HariHaraSudhan :

I think you can Case Function to achieve this below.

from django.db.models import Case, CharField, When
Film.objects.annotate(proxy_rating=Case(
    When(rating=5, then="Excellent"),
    When(rating=4, then="Good"),
    When(rating=3, then="Average"),
    When(rating=2, then="Bad"),
    When(rating=1, then="Very Bad"),
    output_field=CharField()     
    )).values_list("proxy_rating").annotate(count=Count('rating'))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

count all objects within a values_list Django

Django Annotate - get list of distinct field values

django how to annotate to the count in select query values

annotate django query by substring

Django - How to add a value to each tuple in values_list

Django Template: How to display the value of Foreign Key in a values_list

How to add "annotate" data into query with several "values" with django models?

Django - annotate latest child's column value to query

Python/Django - How to annotate a QuerySet with a value determined by another query

Django Query - Annotate With Boolean Value From Date Comparison

Annotate query for calculate sum of 2 table value with Django ORM

Django query to annotate number of foreign keys matching certain value

Django values_list() lowercase

annotate a label to query based on thresholds that it falls within

Django annotate conditional value

using get_context_data() method to retrieve a list of dependant objects within DetailView Django

Django values_list vs values

Does Django makes additional query on accessing prefetched M2M field through "values()" or "values_list()"?

values_list query is returning the foreign keys pk rather than it's value

Create a list using values_list() in django

Retrieve two records within the same table and get datediff in one query

Annotate query set with field value

Efficient Django Query using annotate function

Is there a way to annotate django query with non-expression?

Filter based on counts django annotate query

Django annotate queryset depending on another query

Django annotate average query not showing decimal places

Django - Query : Annotate Queryset with Related Model fields

Django query with annotate and select related together