如何从 Django 过滤器查询中获取总和

时间:2021-04-25 18:47:03

标签: django filter sum

我使用了以下查询,并且正在尝试获取“费用”的总和

import sys
sys.coinit_flags = 0

import wmi
import pythoncom
import time
import threading


def watch_for_modification():
    global GLOBAL_START_FLAG
    print("started")
    pythoncom.CoInitializeEx(sys.coinit_flags)
    try:
        con = wmi.WMI()
        process_watcher = con.Win32_Process.watch_for("modification")
        print("created watcher")
        while GLOBAL_START_FLAG:
            print("8")
            try:
                new_process = process_watcher()
                prev_proc = new_process.previous
                pid = new_process.Processid
                name = new_process.Caption
                write_bytes = new_process.WriteOperationCount
                read_bytes = new_process.ReadOperationCount
                prev_write_bytes = prev_proc.WriteOperationCount
                prev_read_bytes = prev_proc.ReadOperationCount
                if prev_read_bytes != read_bytes or prev_write_bytes != write_bytes:
                    process_to_append = {
                        'pid': pid,
                        'name': name,
                        'write_bytes': write_bytes,
                        'read_bytes': read_bytes,
                        'update_epoch_time': time.time()
                    }
                    print("Modification occurred at", new_process.timestamp)
                    print(new_process.path())
                    print(process_to_append)
                    #Update_list_sem.acquire()
                    #UPDATE_PROCESSES.append(process_to_append)
                    #Update_list_sem.release()

            except wmi.x_wmi_timed_out:
                print(wmi.x_wmi_timed_out)
                pass
    finally:
        pythoncom.CoUninitialize()


t5 = threading.Thread(target=watch_for_modification, daemon=True)
t5.start()
while True:
    pass

但我明白了: {'all_sum':1785}

你知道我是如何获得价值的吗?

3 个答案:

答案 0 :(得分:1)

reg_total_fees =orders.aggregate(all_sum=Sum('par_payment__fee'))['all_sum']

答案 1 :(得分:0)

只需从字典 reg_total_fees 中访问键 'all_sum'。下面的行将为您提供所需的值。

reg_total_fees =orders.aggregate(all_sum=Sum('par_payment__fee'))['all_sum']

答案 2 :(得分:0)

reg_total_fees = orders.objects.filter(写你感兴趣的过滤属性=过滤的预期值).aggregate(Sum('par_payment__fee'))['par_payment__fee__sum']