[Fixed]-Using Case in django

25👍

You Must also Using Django F() and Count() annotate functions…
Try Below code:

Blog.objects.annotate(
    author_titles=Count('author__title'),
    author_likes=Count('author__likes')
).annotate(
    total=F('author_titles') + F('author_likes')
).annotate(
    name=Case(
        When(total__gt=0,total__lt=11, then=Value('B')),
        When(total__gt=10,total__lt=21, then=Value('C')),
        When(total__gt=20,total__lt=31, then=Value('D')),
        default=Value('A'),
        output_field=CharField(),
    )
)

Leave a comment