使用filter,annotate和value的等效Django查询

时间:2014-08-11 07:21:16

标签: python django django-models django-queryset

mysql> select final_category, sum(base) as base, sum(responders) as responders from Johnson_jnjusage
where (bucket="key influencer") and (category1="strong influence") and
(child_age_group = "0 to 12 months") and (sec = "A1") and (no_of_people_house="3")
and (nursing_cnt="1ST TIME MOTHER") and
(city="DELHI" or CITY="HYDERABAD") group by final_category;

我想在django中编写等效查询。现在我在views.py

中做了类似的事情
kwargs1 = {}

if age != None:
    kwargs1['child_age_group__in'] = json.loads(age) #age is the json object converted to list which can have multiple values

if sec != None:
    kwargs1['sec__in'] = json.loads(sec)
if reg != None:
    kwargs1['no_of_people_house__in'] = json.loads(reg)

if nurse != None:
    kwargs1['nursing_cnt__in'] = json.loads(nurse)
if city != None:
    kwargs1['city__in'] = json.loads(city)

if bucket != '' and bucket != None:
    kwargs1['bucket'] = bucket

if category1 != '' and category2 != None:
    kwargs1['category1']  = category1

if category2 != '' and category2 != None:
    kwargs1['category2'] = category2 

我现在正在写的查询是

finalCatBaseResponders = JnJUsage.objects.filter( **kwargs1).values('final_category').annotate(base=Sum('base'), responders = Sum('responders'))

但是我没有得到类似的结果,如果有人能帮助我,我浪费了3天但我无法正确完成。我想从Django查询中得到类似的结果。

MySQL查询的结果是

+----------------------------------------+------+------------+
| final_category                         | base | responders |
+----------------------------------------+------+------------+
|   Friends/acquaintances                |    3 |          0 |
|  Advertising                           |    3 |          1 |
|  Baby care stores                      |    3 |          0 |
|  Baby Club                             |    3 |          0 |
|  Baby-care experts                     |    3 |          0 |
|  Babysitter / Nanny / Daycare Provider |    3 |          0 |
|  By my own experience                  |    3 |          1 |
|  Father                                |    3 |          1 |
|  Friends                               |    3 |          0 |
|  General health workers                |    3 |          0 |
|  General medical practitioners/doctors |    3 |          0 |
|  Husband                               |    3 |          1 |
|  Internet search                       |    3 |          0 |
|  Malish Wali                           |    3 |          0 |
|  Midwives                              |    3 |          0 |
|  Mother                                |    3 |          1 |
|  Mother-in-law                         |    3 |          1 |
|  Newspaper                             |    3 |          0 |
|  Nurses                                |    3 |          0 |
|  Obstetrician-Gynecologist             |    3 |          0 |
|  Online forum                          |    3 |          0 |
|  Other Relatives                       |    3 |          0 |
|  Pediatricians                         |    3 |          1 |
|  Senior Mothers                        |    3 |          0 |
|  Sisters/sisters-in-law                |    3 |          0 |
|  Specialist baby information sites     |    3 |          0 |
|  Website from brands                   |    3 |          0 |
+----------------------------------------+------+------------+

由于

0 个答案:

没有答案