久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Django聚合:僅求和返回值?

Django Aggregation: Sum return value only?(Django聚合:僅求和返回值?)
本文介紹了Django聚合:僅求和返回值?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè)已支付價(jià)值的列表,并希望顯示已支付的總金額.我使用聚合和 Sum 一起計(jì)算值.問(wèn)題是,我只想打印總值,但聚合打印出: {'amount__sum': 480.0} (480.0 是增加的總值.

I have a list of values paid and want to display the total paid. I have used aggregation and Sum to calculate the values together. The problem is,I just want the total value printed out, but aggregation prints out: {'amount__sum': 480.0} (480.0 being the total value added.

在我看來(lái),我有:

    from django.db.models import Sum

    total_paid = Payment.objects.all.aggregate(Sum('amount'))

為了在頁(yè)面上顯示值,我有一個(gè)帶有以下內(nèi)容的 mako 模板:

And to show the value on the page, I have a mako template with the following:

    <p><strong>Total Paid:</strong> ${total_paid}</p>

如何讓它顯示 480.0 而不是 {'amount__sum': 480.0}?

How would I get it to show 480.0 instead of {'amount__sum': 480.0}?

推薦答案

我不相信有辦法只獲得價(jià)值.

I don't believe there is a way to get only the value.

您可以在模板中執(zhí)行 ${{ total_paid.amount__sum }}.或者在你的視圖中執(zhí)行 total_paid = Payment.objects.all().aggregate(Sum('amount')).get('amount__sum', 0.00).

You could just do ${{ total_paid.amount__sum }} in your template. Or do total_paid = Payment.objects.all().aggregate(Sum('amount')).get('amount__sum', 0.00) in your view.

編輯

正如其他人指出的那樣,.aggregate() 將始終返回一個(gè)字典,其中包含來(lái)自聚合的所有鍵,因此在 .get() 上執(zhí)行結(jié)果是不必要的.但是,如果查詢集為空,則每個(gè)聚合值將是 None.因此,根據(jù)您的代碼,如果您期望浮動(dòng),您可以這樣做:

As others have pointed out, .aggregate() will always return a dictionary with all of the keys from the aggregates present, so doing .get() on the result is not necessary. However, if the queryset is empty, each aggregate value would be None. So depending on your code, if you are expecting a float, you could do:

total_paid = Payment.objects.all().aggregate(Sum('amount'))['amount__sum'] 或 0.00

這篇關(guān)于Django聚合:僅求和返回值?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 99久久久国产精品免费蜜臀 | 亚欧洲精品在线视频免费观看 | 亚洲啪啪网 | 97国产超碰 | 少妇综合| 亚洲欧美日本在线 | 91av视频 | 超碰97免费 | 久久伊人影院 | 国产主播99 | 成人扒开伸进免费观看 | 免费在线观看黄 | 亚洲欧美日韩成人 | 女人高潮特级毛片 | 免费一区 | 在线观看视频91 | 黄www. | 人人草人人 | 国产精品欧美一区二区 | 国产成人午夜 | 久久伊人精品 | 中文字幕av久久爽一区 | 黄网站免费大全入口 | 日日夜夜狠狠操 | 极品美女一区二区三区 | 免费一看一级毛片 | 国产欧美一区二区三区视频在线观看 | 日韩一区二区在线播放 | 四虎影视最新网址 | 成人久久av | 国产理论片在线观看 | 国产午夜精品视频 | 成年免费视频黄网站在线观看 | 深夜免费福利 | 欧美日韩亚洲一区二区 | 亚洲精品乱码 | 精品国产91 | www一级片| 欧美日韩少妇 | 国产精品国产精品国产专区不卡 | 欧美精品二区三区四区免费看视频 |