Django queryset聚合计数按多列分组

时间:2015-02-16 22:15:15

标签: python mysql django

我正在尝试使用我的表' SystemExceptionLog'中的Django查询集有效地创建一个计数列。我想按上传文件,证据和cafae_field进行分组。而且我想要计算名为' errormsg'的字段。 我的问题是,我认为我可能需要添加'值()'到该行获取聚合的不同值,因为这些行是多行枚举的一部分。我想知道我怎么做 1.获取计数仅用于'证据' cafae_field'的唯一组合。和'上传文件'? 2.只获得不同的价值观?

    err_obj=SystemExceptionLog.objects.filter(uploadedfile=extract_properties['uploaded_file'], cafae_field=extract_properties['cafae_artifact'], evidence=evidence_obj).aggregate(Count('errormsg'))
    err_ct = err_obj['errormsg__count']

以下是循环的完整代码,其中还包含上面的行......

def process_tzworks_lnk(extract_properties):
'''
Processes extract from TZWorks lnk extract. 
'''
case_obj, created = Case.objects.get_or_create(case_number=extract_properties['case_number'])
evidence_obj, created = Evidence.objects.get_or_create(case=case_obj, evidence_number=extract_properties['evidence_number'])
err_obj=SystemExceptionLog.objects.filter(uploadedfile=extract_properties['uploaded_file'], cafae_field=extract_properties['cafae_artifact'], evidence=evidence_obj).aggregate(Count('errormsg'))
err_ct = err_obj['errormsg__count']
datetime_obj = SystemExceptionLog.objects.filter(uploadedfile=extract_properties['uploaded_file'], evidence=evidence_obj).aggregate(Max('timestamp'))
date_err = datetime_obj['timestamp__max']
md5hash = hashlib.md5(open('nettop/files/name.csv', 'rb').read()).hexdigest()

reader = unicode_csv_reader(extract_properties['uploaded_file'], dialect='pipes')

print "num",extract_properties['header_row_num']   
for idx, row in enumerate(reader):
    if idx <= (extract_properties['header_row_num'])+3: 
        pass
    else:
        linkkeys = get_tzworks_lnk(row)
        try:
            file_shortcut_obj, created = FileShortcut.objects.get_or_create(
                evidence=evidence_obj, 
                volume=None,
                sourcepath_filename=row[0],
                source_type=row[1],
                file_mdate=date_convert(row[2],row[3]), 
                file_adate=date_convert(row[4],row[5]),
                file_cdate=date_convert(row[6],row[7]),
                target_mdate=date_convert(row[8],row[9]),
                target_adate=date_convert(row[10],row[11]),  
                target_cdate=date_convert(row[12],row[13]),
                object_id_cdate=date_convert(row[14],row[15]),
                target_attribute=row[16],  #threw err
                inode=row[17],
                sequence_nbr=row[18],
                file_size=row[19],
                target_name=row[20],
                idlist_extra_info=row[21],
                volume_type=row[22],
                volume_serial=row[23],
                volume_name=row[24],
                local_path=row[25],
                common_path=row[26],
                network_device_info=row[27],
                description=linkkeys['Description'],
                relative_path=linkkeys['RelPath'],
                command_arguments=linkkeys['CmdArgs'],
                working_directory=linkkeys['WorkDir'],
                icon_name=linkkeys['IconName'],
                environment_block=linkkeys['EnvBlk'],
                netbios_name=row[29],
                volumes_id=row[30],
                object_id=row[31],
                mac_address=row[32],)
        except Exception as e:
            print "Error",e
            print "row",row
            print "idx:",idx
            #import code; code.interact(local=locals())
            # These will create the items for the EvidenceUpload table on artefact's upload status & the SystemExceptionLog, for when Exceptions occur during Processor
            Ex, created = SystemExceptionLog.objects.get_or_create(evidence=evidence_obj, md5hash=md5hash, indexrow=idx, errormsg=e.args[0], timestamp=datetime.datetime.now(), uploadedfile=extract_properties['uploaded_file'],\
            cafae_field=extract_properties['cafae_artifact'])
            continue
err_obj=SystemExceptionLog.objects.filter(uploadedfile=extract_properties['uploaded_file'], evidence=evidence_obj).aggregate(Count('errormsg'))
err_ct = err_obj['errormsg__count']
Ev, created = EvidenceUpload.objects.get_or_create(evidence=evidence_obj, md5hash=md5hash, cafae_field=extract_properties['cafae_artifact'], file_name=extract_properties['uploaded_file'],\
nbr_errors=err_ct, rec_upload_attempt=datetime.datetime.now(), uploaded_by=User)  

return True

0 个答案:

没有答案