Django查询集聚合不像我认为的那样工作

时间:2014-04-22 18:36:06

标签: django

以下代码片段旨在从数据库中形成一个基本查询集,可以对其进行多次计算:

basicQuerySet = Job.objects.filter(g_end_time__gte=epochTimes[0]) \
     .filter(g_end_time__lt=epochTimes[12]) \
     .order_by('g_user__g_affiliation') \
     .values('g_job_id','g_user_id','g_project','g_machine','g_cpu_days', \
              'g_user__g_affiliation','g_user__g_user','g_user__g_organization', \
              'g_user__g_common_name')

# get all g_affiliations during the past twelve months and convert to a list
interimList = basicQuerySet.values('g_user__g_affiliation').distinct()
affiliationList = []
for i,u in enumerate(interimList):
    if u['g_user__g_affiliation'] is not None:
        affiliationList.append(u['g_user__g_affiliation'])

# For each affiliation, get data
affiliationUsageData = {}
for m in range(len(affiliationList)):
    thisAffiliation = affiliationList[m]
    thisAffiliationJobs = basicQuerySet.filter(g_user__g_affiliation=thisAffiliation).count()
    thisAffiliationUsers = basicQuerySet.filter(g_user__g_affiliation=thisAffiliation).values('g_user_id').distinct().count()
    interimNumber = basicQuerySet.filter(g_user__g_affiliation=thisAffiliation).aggregate(Sum('g_cpu_days'))
    thisAffiliationDays = interimNumber['g_cpu_days__sum']
    affiliationUsageData[thisAffiliation] = []
    thisData = {'jobs':thisAffiliationJobs,'users':thisAffiliationUsers,'days':thisAffiliationDays}
    affiliationUsageData[thisAffiliation].append(thisData)

不起作用的部分是汇总以获得' g_cpu_days'的总和。

' basicQuerySet'确实与预期的字段一起返回。 ' thisAffiliationJobs'的计数返回正确的数字。 < thisAffiliationUsers'的计数返回正确的数字。 但是,聚合返回

interimNumber = {}

一本空字典。

如果我创建一个直接进行聚合的新查询集

    interimNumber = Job.objects.filter(g_end_time__gte=epochTimes[0]) \
     .filter(g_end_time__lt=epochTimes[12]).filter(g_user__g_affiliation=thisAffiliation).aggregate(Sum('g_cpu_days'))
    thisAffiliationDays = interimNumber['g_cpu_days__sum']

然后返回预期的金额。

显然,我需要接受一些教育。是否可以在查询集上执行聚合(Sum)而无需再次访问数据库?

这是在DJango v1.6中运行。

提前感谢您的帮助(以及此代码段的任何其他指示,因为我对Django来说很新)!

1 个答案:

答案 0 :(得分:0)

我还没有回答这个问题,但我认为每个人都可以从一些造型帮助中受益:

basicQuerySet = Job.objects.
    filter(g_end_time__gte=epochTimes[0]). \
    filter(g_end_time__lt=epochTimes[12]). \
    order_by('g_user__g_affiliation'). \
    values(
        'g_job_id',
        'g_user_id',
        'g_project',
        'g_machine',
        'g_cpu_days',      
        'g_user__g_affiliation',
        'g_user__g_user',
        'g_user__g_organization', 
        'g_user__g_common_name'
    )

# get all g_affiliations during the past twelve months and convert to a list
interimList = basicQuerySet.values('g_user__g_affiliation').distinct()

affiliationList = []

for i,u in enumerate(interimList):
    if u['g_user__g_affiliation'] is not None:
        affiliationList.append(u['g_user__g_affiliation'])

# For each affiliation, get data
affiliationUsageData = {}

for m in range(len(affiliationList)):
    thisAffiliation = affiliationList[m]

    thisAffiliationJobs = basicQuerySet.
        filter(g_user__g_affiliation=thisAffiliation).\
        count()

    thisAffiliationUsers = basicQuerySet.\
        filter(g_user__g_affiliation=thisAffiliation).\
        values('g_user_id').\
        distinct().\
        count()

    interimNumber = basicQuerySet.\
        filter(g_user__g_affiliation=thisAffiliation).\
        aggregate(Sum('g_cpu_days'))

    thisAffiliationDays = interimNumber['g_cpu_days__sum']
    affiliationUsageData[thisAffiliation] = []

    thisData = {
        'jobs':thisAffiliationJobs,
        'users':thisAffiliationUsers,
        'days':thisAffiliationDays
    }

    affiliationUsageData[thisAffiliation].append(thisData)