gpt4 book ai didi

python - Django 中的过滤器值显示奇怪

转载 作者:行者123 更新时间:2023-12-01 07:29:47 25 4
gpt4 key购买 nike

我正在使用过滤器和 .aggregate 来汇总 Manifests 模型中 cases 列的值。当它显示在模板中时,它会给出正确的金额,但是当页面显示时,它会显示为例如 {'cases__sum': 1192}。数字 1192 确实是总和,但我不希望其余文本显示给用户!我怎样才能阻止这个并只获取号码?

views.py

def add_manifest(request, reference_id):
form = CreateManifestForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
instance = form.save(commit=False)
try:
order = Orders.objects.get(id=reference_id)
instance.reference = order
except Orders.DoesNotExist:
pass
instance.save()
form = CreateManifestForm(initial={'reference': Orders.objects.get(reference=reference_id)})
reference = request.POST.get('reference')
manifests = Manifests.objects.all().filter(reference=reference)
total_cases = Manifests.objects.filter(reference=reference).aggregate(Sum('cases')) #totaling the cases for the readonly field
totalCNF = 0
for item in manifests:
totalCNF += item.cases * item.CNF

context = {
'form': form,
'reference_id': reference_id,
'manifests' : manifests,
'total_cases': total_cases,
'totalCNF': totalCNF,

}

return render(request, 'add_manifest.html', context)

add_manifest.html

  <label for="form.reference" class="formlabels">Case Total:</label><br>
<input type="text" value="{{ total_cases }}" readonly>

我只想在此 HTML 输入框中显示数字,而不是整个引用

最佳答案

聚合值存储在字典中。您可以使用

{{ total_cases.cases__sum }}

仅显示值,或在构建上下文变量时执行此操作:

...
'total_cases': total_cases['cases__sum'],

关于python - Django 中的过滤器值显示奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57262912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com