def sql_count_when(*qargs, **kwargs):
    ' qargs : list of models.Q objects\n        kwargs : filter_term=value dict\n    '
    condition = db.Q(**kwargs)
    for q in qargs:
        condition &= q
    return db.Count(db.Case(db.When(condition, then=1), output_field=db.IntegerField()))
qargs : list of models.Q objects
kwargs : filter_term=value dict
def sql_count_when(*qargs, **kwargs):
    ' qargs : list of models.Q objects\n        kwargs : filter_term=value dict\n    '
    condition = db.Q(**kwargs)
    for q in qargs:
        condition &= q
    return db.Count(db.Case(db.When(condition, then=1), output_field=db.IntegerField()))