Skip to content

Instantly share code, notes, and snippets.

@mazlum
Created June 3, 2019 14:30
Show Gist options
  • Save mazlum/6b2a4c2bb86e2bb7ed07507ce8d94368 to your computer and use it in GitHub Desktop.
Save mazlum/6b2a4c2bb86e2bb7ed07507ce8d94368 to your computer and use it in GitHub Desktop.

Revisions

  1. mazlum created this gist Jun 3, 2019.
    18 changes: 18 additions & 0 deletions making_queries_q1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    from django.db.models import Case, IntegerField, Sum, Value, When
    from django.db.models.functions import Coalesce

    q = Exam.objects.aggregate(
    success=Coalesce(
    Sum(
    Case(
    When(score__gte=85, then=Value(1)),
    default=Value(0),
    output_field=IntegerField(),
    )
    ),
    Value(0),
    ),
    )

    print(q)
    # {"success": 5}