Django总和的乘法值 [英] Django Sum of Multiplied Values
问题描述
每个资产都有重量和数量,我可以显示总重量每个资产如下:
def by_item_weight(self):
此方法给出资产重量* Qnty
total = self.asset_quantity * self.asset_weight
返回总额
我想做的是拿每个资产的总项目权重(qnty *权重的结果),并显示所有资产的所有重量。
我已经尝试使用以下各种各样的组合来返回by_item_weight'列'的总和:
资产.objects.all()。注释(total = Sum('by_item_weight')
但是我明白了不会在模型资产中定义按项目重量。
我通过for循环呈现表格,如下所示,我的资产st.html模板:
{%block content%}
< table class =well table table-striped text-center>
< thead>
< tr class =text-center>
< th class =text-center>资产ID:< / th>
< th class =text-center>资产名称:< / th>
< th class =text-center>资产数量:< / th>
< th class =text-center>资产重量/ kg:< / th>
< th class =text-center>总重量/ kg:< / th>
< th class =text-center>资产所有者:< / th>
< / tr>
< / thead>
< tbody>
< tr class =text-center>
{%for object_list%}
< td> {{asset.id}}< / td>
< td> {{asset.asset_name}}< / td>
< td> {{asset.asset_quantity}}< / td>
< td> {{asset.asset_weight}}< / td>
< td> {{asset.by_item_weight}}< / td>
< td> {{asset.asset_owner}}< / td>
< / tr>
{%endfor%}
我是新来的Python,并且无法找到一种方式来捕获每个asset.by_item_weight的值。如果我可以在一个列表中捕获我想我可以将它们列在列表中并显示结果。
我的模型
class Asset(models.Model):
asset_name = models.CharField(max_length = 30 )
asset_quantity = models.IntegerField(default = 0)
asset_weight = models.IntegerField(default = 0)
asset_owner = models.ForeignKey(
'AssetOwner',
on_delete = models.CASCADE,
)#这应该是AssetOwners owner_name的外键下拉菜单。
def by_item_weight(self):
此方法给出资产的总价值Weight * Qnty
total = self.asset_quantity * self.asset_weight
返回总额
def __str __(self):
return'{}'format(self.asset_name)
return'资产数量:{ }'。format(self.asset_quantity)
return'资产重量:{}'。format(self.asset_weight)
return'资产所有者:{}'。format(self.asset_owner)
返回资产所有者:{}'。format(self.asset_owner)
任何帮助都会很多赞赏。
更新:
现在没有错误,但仍然无法显示/显示sum_total的值
新模板
{%extendspersonal / header.html %
{%block content%}
< h1 class ='text-center'>这是完全资产列表,不被所有者< / h1> ;< ; / BR>
< table class =well table table-striped text-center>
< thead>
< tr class =text-center>
< th class =text-center>资产ID:< / th>
< th class =text-center>资产名称:< / th>
< th class =text-center>资产数量:< / th>
< th class =text-center>资产重量/ kg:< / th>
< th class =text-center>总重量/ kg:< / th>
< th class =text-center>资产所有者:< / th>
< / tr>
< / thead>
< tbody>
< tr class =text-center>
{%for object_list%}
< td> {{asset.id}}< / td>
< td> {{asset.asset_name}}< / td>
< td> {{asset.asset_quantity}}< / td>
< td> {{asset.asset_weight}}< / td>
< td> {{asset.by_item_weight}}< / td>
< td> {{asset.asset_owner}}< / td>
< / tr>
{%endfor%}
< / tbody>
< / table>
< p class =style =>总存货量:{{assets.sum_total}}< / p>
< p class =text-center> {%includesam / includes / backtosam.html%}< / p>
{%endblock%}
新模型
<_ c $ c>从__future__导入unicode_literals
从django.db导入模型
从django.db.models导入Sum,F,计数
从django.db.models导入最大
从django.db.models导入ExpressionWrapper
从django.db.models导入汇总
class Asset(models.Model):
asset_name = models.CharField(max_length = 30)
asset_quantity = models.IntegerField(default = 0)
asset_weight = models.IntegerField(default = 0)
asset_owner = models.ForeignKey(
'AssetOwner',
on_delete = models.CASCADE,
)#这应该是AssetOwners owner_name的外键下拉菜单。
def by_item_weight(self):
此方法给出资产的总价值重量* Qnty
total = self.asset_quantity * self.asset_weight
返回总额
def sum_total(self):
assets = Asset.objects.all()。annotate(
total_weight = ExpressionWrapper(F('asset_quantity')* F asset_weight'),output_field = IntegerField)
the_sum = assets.aggregate(total = Sum('total_weight'))
返回the_sum
def __str __(self):
return'{}'。format(self.asset_name)
return'资产数量:{}'。format(self.asset_quantity)
返回'资产重量:{}'。资产所有者:{}'。format(self.asset_owner)
返回'资产所有者:{}'。格式(self.asset_owner)
返回资产所有者:{} :{}'。format(self.assets)
更新视图
从django.shortcuts导入render
从django.http导入HttpResponse
从django.core.cache导入缓存
from django.db.models import Sum,F
def get_total_weight(self):
total_weight = cache.get('total_weight', - 1)
如果total_weight == -1:
total_weight = Asset.objects.annotate(total_weight = F('asset_quantity')* F('asset_weight'))aggregate(total = Sum('total_weight'))
#这是测试
cache.set('total_weight',total_weight)
返回total_weight
def索引(请求):
返回渲染(请求'sam / index.html')
def assetslist(request):
return render(request,'sam / assetslist.html',{'total_weight' :get_total_weight},assets = Asset.objects.all())
我怀疑有一个问题以上资产清单方法我不明显地叫。
e
{%extendspersonal / header.html%}
{%block content% }
< h1 class ='text-center'>这是完全资产列表不由所有者分割< / h1>< / br>
< table class =well table table-striped text-center>
< thead>
< tr class =text-center>
< th class =text-center>资产ID:< / th>
< th class =text-center>资产名称:< / th>
< th class =text-center>资产数量:< / th>
< th class =text-center>资产重量/ kg:< / th>
< th class =text-center>总重量/ kg:< / th>
< th class =text-center>资产所有者:< / th>
< / tr>
< / thead>
< tbody>
< tr class =text-center>
{%for object_list%}
< td> {{asset.id}}< / td>
< td> {{asset.asset_name}}< / td>
< td> {{asset.asset_quantity}}< / td>
< td> {{asset.asset_weight}}< / td>
< td> {{asset.by_item_weight}}< / td>
< td> {{asset.asset_owner}}< / td>
< / tr>
{%endfor%}
< / tbody>
< / table>
< p class =style =>总重量有库存:{{get_total_weight}}< / p>
< p class =style =>总存货量:{{assetslist}}< / p>
<! - < table class =well table table-stripe text-center>
< thead>
< tr class =text-center>
< th class =text-center>< / th>
< th class =text-center>< / th>
< th class =text-center>< / th>
< th class =text-center>< / th>
< th class =text-center>总重量/ kg:< / th>
< / tr>
< / thead>
< tbody>
< tr class =text-center>
{%for object_list%}中的sum_weight
< td>< / td>
< td>< / td>
< td>< / td>
< td>< / td>
< td> {{assets.sum_total}}< / td>
< / tr>
{%endfor%}
< / tbody>
< / table> - >
< p class =text-center> {%includesam / includes / backtosam。 html%}< / p>
{%endblock%}
感谢任何投入/建议。
进一步更新:
我已将视图调整如下:
from django.core.cache import cache
from django.db.models import Sum,F
def get_total_weight(self):
total_weight = cache.get('total_weight', - 1)
如果total_weight == -1:
total_weight = Asset.objects.annotate(total_weight = F 'asset_quantity')* F('asset_weight'))。aggregate(total = Sum('total_weight'))
#这是测试
cache.set('total_weight',total_weight)
返回total_weight
呈现(请求,'template_name',{'total_weight':get_total_weight,assets = Asset.objects.all()})
我在 assets = Asset.objects.all()}上收到错误)
=标志。语法错误:无效的语法
我认为渲染需要在自己的功能中?
更新: p>
我已经更新了我的观点,并将def从models.py
我的views.py文件如下
def total_weight(request):
assets = Asset.objects.all()。annotate
total_weight = ExpressionWrapper(F('asset_quantity')* F('asset_weight'),
output_field = IntegerField()))
return render(request,'sam / index
def sum_total(request):
the_total = assets.aggregate(total = Sum('total_weight'))
return render(request,'sam / assetlist.html')
def index(request):
return render(request,'sam / index.html' )
def by_item_weight(self):
此方法给出资产的总价值重量* Qnty
total = sel f.asset_quantity * self.asset_weight
return total
def get_total_weight(self):
total_weight = Asset.objects.filter(by_item_weight__isnull = True).aggregate(Sum('by_item_weight '))
Asset.objects.annotate(total_weight = F('asset_quantity')* F('asset_weight'))aggregate(total = Sum('total_weight'))
def __str __(self):
return'{}'format(self.asset_name)
return'{}'format(self.total_weight)
assetlist.html
以下是我试图实现的JS来解决这个问题问题不起作用。
我觉得我在assetslist.html上缺少一些在调用views.py中的def值。
{%extendspersonal / header.html%}
{%block content% }
< h1 class ='text-center'>这是完全资产列表不由所有者分割< / h1>< / b R>
< table id =sum_tableclass =well table table-striped text-center>
< thead>
< tr class =text-center titlerow>
< td class =text-center>资产ID:< / td>
< td class =text-center>资产名称:< / td>
< td class =text-center>资产数量:< / td>
< td class =text-center>资产重量/ kg:< / td>
< td class =text-center>总重量/ kg:< / td>
< td class =text-center>资产所有者:< / td>
< / tr>
< / thead>
< tbody>
< tr class =text-center>
{%for object_list%}
< td>< a href =/ sam / assets / {{asset.id}}> {{asset.id }}< />< / TD>
< td> {{asset.asset_name}}< / td>
< td> {{asset.asset_quantity}}< / td>
< td> {{asset.asset_weight}}< / td>
< td class =rowDataSd> {{asset.by_item_weight}}< / td>
< td>< a href =/ sam / owners /> {{asset.asset_owner}}< />< / td>
< / tr>
{%endfor%}
< tr class =totalColumn>
< td class =>< / td>
< td class =>< / td>
< td class =>< / td>
< td class =>< / td>
< td class =totalCol>总计:{{asset.get_total_weight}}< / td>
< td class =>< / td>
< / tr>
< / tbody>
< / table>
< p>希望这是满的({{this_view}})?< / p>
< p class =text-center> {%includesam / includes / backtosam.html%}< / p>
<! -
< script>
var total = 0;
$('#sum_table tr td.rowDataSd')。each(function(){
total + = parseInt($(this).text());
});
$('#sum_table td.totalCol')。text(total:+ total);
< / script>
- >
{%endblock%}
更新 - 2016年7月3日
从__future__导入unicode_literals
从django.db导入模型
从django.db.models导入Sum,F,从django.db.models导入
从django.db.models导入Max
从django.db.models导入ExpressionWrapper
从django.contrib导入Aggregate
。 auth.models import User
class Asset(models.Model):
asset_name = models.CharField(max_length = 30)
asset_quantity = models.IntegerField(default = 0)
asset_weight = models.IntegerField(default = 0)
total_assets_weight = models.IntegerField(default = 0)
asset_owner = models.ForeignKey(
'AssetOwner',
on_delete = models.CASCADE,
)#这应该是AssetOwners owner_name的外键下拉。
def by_item_weight(self):
此方法给出资产的总价值重量* Qnty
total = self.asset_quantity * self.asset_weight
return total
def total_weight(self):
assets = Asset.objects.all()。annotate(
total_weight = ExpressionWrapper 'asset_quantity')* F('asset_weight'),
output_field = IntegerField())
the_total = assets.aggregate(total = Sum('total_weight'))
返回the_total
模板
{%extendspersonal / header.html%}
{%block content%}
< h1 class ='text-center'>这是完全资产列表不被所有者分割< / h1>< / br>
< table id =sum_tableclass =well table table-striped text-center>
< thead>
< tr class =text-center titlerow>
< td class =text-center>资产ID:< / td>
< td class =text-center>资产名称:< / td>
< td class =text-center>资产数量:< / td>
< td class =text-center>资产重量/ kg:< / td>
< td class =text-center>总重量/ kg:< / td>
< td class =text-center>资产所有者:< / td>
< / tr>
< / thead>
< tbody>
< tr class =text-center>
{%for object_list%}
< td>< a href =/ sam / assets / {{asset.id}}> {{asset.id }}< / TD>
< td> {{asset.asset_name}}< / td>
< td> {{asset.asset_quantity}}< / td>
< td> {{asset.asset_weight}}< / td>
< td class =rowDataSd> {{asset.by_item_weight}}< / td>
< td>< a href =/ sam / owners /> {{asset.asset_owner}}< / td>
< / tr>
{%endfor%}
{%for object_list%}
< tr class =totalColumn>
< td class =>< / td>
< td class =>< / td>
< td class =>< / td>
< td class =>< / td>
< td class =totalCol>总计:{{total.total_weight}}< / td>
< td class =>< / td>
< / tr>
< / tbody>
< / table>
{%endfor%}
< p class =text-center> {%includesam / includes / backtosam.html%} p为H.
{%endblock%}
您可以使用 ExpressionWrapper() (主要是Django 1.8 +)
assets = Asset.objects.all()。注释(
total_weight = ExpressionWrapper(F('asset_quantity')* F('asset_weight'),
output_field = IntegerField())
这应该给你每个对象的总重量,即数量倍数。
现在,你应该可以从所有 total_weight
中获得总额。
编辑现在你可以使用综合来获取总计
assets.aggregate(total = Sum('total_weight'))
{'total':1234.5678}
I am trying to show the total weight of assets I have presented on a table.
Each Asset has a weight and a quantity and I can present the total weight of each asset as below:
def by_item_weight(self):
"""This Method gives the total value of the asset. Weight * Qnty"""
total = self.asset_quantity * self.asset_weight
return total
What I would like to do is take the total item weight of each asset (result of qnty * weight) and present the over all weight of all assets.
I have tried to make various combinations of the below to return the sum of the by_item_weight 'column':
Asset.objects.all().annotate(total=Sum('by_item_weight')
But I understand this will not work as by-item-weight is not defined in the model Asset.
I am presenting the table via a for loop as below on my assetslist.html template:
{% block content %}
<table class="well table table-striped text-center">
<thead>
<tr class="text-center">
<th class="text-center">Asset ID:</th>
<th class="text-center">Asset Name:</th>
<th class="text-center">Asset Quantity:</th>
<th class="text-center">Asset Weight / kg:</th>
<th class="text-center">Total Weight / kg:</th>
<th class="text-center">Asset Owner:</th>
</tr>
</thead>
<tbody>
<tr class="text-center">
{% for asset in object_list %}
<td>{{ asset.id }}</td>
<td>{{ asset.asset_name }}</td>
<td>{{ asset.asset_quantity }}</td>
<td>{{ asset.asset_weight }}</td>
<td>{{ asset.by_item_weight }}</td>
<td>{{ asset.asset_owner }}</td>
</tr>
{% endfor %}
I am new to Python and am having trouble finding a way to capture the value of each asset.by_item_weight which If I could capture in a list I presume I could them sum the list and show the result.
My Models
class Asset(models.Model):
asset_name = models.CharField(max_length=30)
asset_quantity = models.IntegerField(default=0)
asset_weight = models.IntegerField(default=0)
asset_owner = models.ForeignKey(
'AssetOwner',
on_delete=models.CASCADE,
) # This should be a Foreign Key Drop down of AssetOwners owner_name.
def by_item_weight(self):
"""This Method gives the total value of the asset. Weight * Qnty"""
total = self.asset_quantity * self.asset_weight
return total
def __str__(self):
return '{}'.format(self.asset_name)
return 'Asset Quantity: {}'.format(self.asset_quantity)
return 'Asset Weight: {}'.format(self.asset_weight)
return 'Asset Owner: {}'.format(self.asset_owner)
return 'Asset Owner: {}'.format(self.asset_owner)
Any help would be much appreciated.
UPDATED:
No Errors now, but still not able to show/display the value of sum_total
New Template
{% extends "personal/header.html" %}
{% block content %}
<h1 class='text-center'>This is the full asset list not split by owner</h1></br>
<table class="well table table-striped text-center">
<thead>
<tr class="text-center">
<th class="text-center">Asset ID:</th>
<th class="text-center">Asset Name:</th>
<th class="text-center">Asset Quantity:</th>
<th class="text-center">Asset Weight / kg:</th>
<th class="text-center">Total Weight / kg:</th>
<th class="text-center">Asset Owner:</th>
</tr>
</thead>
<tbody>
<tr class="text-center">
{% for asset in object_list %}
<td>{{ asset.id }}</td>
<td>{{ asset.asset_name }}</td>
<td>{{ asset.asset_quantity }}</td>
<td>{{ asset.asset_weight }}</td>
<td>{{ asset.by_item_weight }}</td>
<td>{{ asset.asset_owner }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<p class="" style="">Total Weight In Stock : {{ asset.sum_total }}</p>
<p class="text-center">{% include "sam/includes/backtosam.html" %}</p>
{% endblock %}
New Models
from __future__ import unicode_literals
from django.db import models
from django.db.models import Sum, F, Count
from django.db.models import Max
from django.db.models import ExpressionWrapper
from django.db.models import Aggregate
class Asset(models.Model):
asset_name = models.CharField(max_length=30)
asset_quantity = models.IntegerField(default=0)
asset_weight = models.IntegerField(default=0)
asset_owner = models.ForeignKey(
'AssetOwner',
on_delete=models.CASCADE,
) # This should be a Foreign Key Drop down of AssetOwners owner_name.
def by_item_weight(self):
"""This Method gives the total value of the asset. Weight * Qnty"""
total = self.asset_quantity * self.asset_weight
return total
def sum_total(self):
assets = Asset.objects.all().annotate(
total_weight=ExpressionWrapper(F('asset_quantity') * F('asset_weight'),output_field=IntegerField))
the_sum = assets.aggregate(total=Sum('total_weight'))
return the_sum
def __str__(self):
return '{}'.format(self.asset_name)
return 'Asset Quantity: {}'.format(self.asset_quantity)
return 'Asset Weight: {}'.format(self.asset_weight)
return 'Asset Owner: {}'.format(self.asset_owner)
return 'Asset Owner: {}'.format(self.asset_owner)
return 'Total Weight of Assets: {}'.format(self.assets)
Updated view
from django.shortcuts import render
from django.http import HttpResponse
from django.core.cache import cache
from django.db.models import Sum, F
def get_total_weight(self):
total_weight = cache.get('total_weight',-1)
if total_weight == -1:
total_weight = Asset.objects.annotate(total_weight=F('asset_quantity')*F('asset_weight')).aggregate(total=Sum('total_weight'))
# this is tested
cache.set('total_weight',total_weight)
return total_weight
def index(request):
return render(request, 'sam/index.html')
def assetslist(request):
return render(request,'sam/assetslist.html',{'total_weight': get_total_weight}, assets = Asset.objects.all())
I suspect there is an issue with the above assetslist method which I am not apparently calling.
Template
{% extends "personal/header.html" %}
{% block content %}
<h1 class='text-center'>This is the full asset list not split by owner</h1></br>
<table class="well table table-striped text-center">
<thead>
<tr class="text-center">
<th class="text-center">Asset ID:</th>
<th class="text-center">Asset Name:</th>
<th class="text-center">Asset Quantity:</th>
<th class="text-center">Asset Weight / kg:</th>
<th class="text-center">Total Weight / kg:</th>
<th class="text-center">Asset Owner:</th>
</tr>
</thead>
<tbody>
<tr class="text-center">
{% for asset in object_list %}
<td>{{ asset.id }}</td>
<td>{{ asset.asset_name }}</td>
<td>{{ asset.asset_quantity }}</td>
<td>{{ asset.asset_weight }}</td>
<td>{{ asset.by_item_weight }}</td>
<td>{{ asset.asset_owner }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<p class="" style="">Total Weight In Stock : {{ get_total_weight }}</p>
<p class="" style="">Total Weight In Stock : {{ assetslist }}</p>
<!-- <table class="well table table-striped text-center">
<thead>
<tr class="text-center">
<th class="text-center"></th>
<th class="text-center"></th>
<th class="text-center"></th>
<th class="text-center"></th>
<th class="text-center">Total Weight / kg:</th>
</tr>
</thead>
<tbody>
<tr class="text-center">
{% for sum_weight in object_list %}
<td></td>
<td></td>
<td></td>
<td></td>
<td>{{ asset.sum_total }}</td>
</tr>
{% endfor %}
</tbody>
</table> -->
<p class="text-center">{% include "sam/includes/backtosam.html" %}</p>
{% endblock %}
Thanks for any input/suggestions.
Further UPDATE:
I have adjusted the view to the below:
from django.core.cache import cache
from django.db.models import Sum, F
def get_total_weight(self):
total_weight = cache.get('total_weight',-1)
if total_weight == -1:
total_weight = Asset.objects.annotate(total_weight=F('asset_quantity')*F('asset_weight')).aggregate(total=Sum('total_weight'))
# this is tested
cache.set('total_weight',total_weight)
return total_weight
render(request,'template_name',{'total_weight': get_total_weight, assets = Asset.objects.all() } )
I am getting errors on the assets = Asset.objects.all() } )
= sign. SyntaxError: invalid syntax
I presume that render needs to be in its own function?
UPDATE:
I have updated my views and moved the def from the models.py
my views.py file is as below
def total_weight(request):
assets = Asset.objects.all().annotate(
total_weight=ExpressionWrapper(F('asset_quantity') * F('asset_weight'),
output_field=IntegerField() ))
return render(request, 'sam/index.html')
def sum_total(request):
the_total = assets.aggregate(total=Sum('total_weight'))
return render(request, 'sam/assetlist.html')
def index(request):
return render(request, 'sam/index.html')
def by_item_weight(self):
"""This Method gives the total value of the asset. Weight * Qnty"""
total = self.asset_quantity * self.asset_weight
return total
def get_total_weight(self):
total_weight = Asset.objects.filter(by_item_weight__isnull=True).aggregate(Sum('by_item_weight'))
Asset.objects.annotate(total_weight=F('asset_quantity')*F('asset_weight')).aggregate(total=Sum('total_weight'))
def __str__(self):
return '{}'.format(self.asset_name)
return '{}'.format(self.total_weight)
assetlist.html
There is JS below that I tried to implement to resolve this issue which did not work either.
I feel like I am missing something on the assetlist.html in calling the values of the def in the views.py.
{% extends "personal/header.html" %}
{% block content %}
<h1 class='text-center'>This is the full asset list not split by owner</h1></br>
<table id="sum_table" class="well table table-striped text-center">
<thead>
<tr class="text-center titlerow">
<td class="text-center">Asset ID:</td>
<td class="text-center">Asset Name:</td>
<td class="text-center">Asset Quantity:</td>
<td class="text-center">Asset Weight / kg:</td>
<td class="text-center">Total Weight / kg:</td>
<td class="text-center">Asset Owner:</td>
</tr>
</thead>
<tbody>
<tr class="text-center">
{% for asset in object_list %}
<td><a href="/sam/assets/{{ asset.id }}">{{ asset.id }}</></td>
<td>{{ asset.asset_name }}</td>
<td>{{ asset.asset_quantity }}</td>
<td>{{ asset.asset_weight }}</td>
<td class="rowDataSd">{{ asset.by_item_weight }}</td>
<td><a href="/sam/owners/">{{ asset.asset_owner }}</></td>
</tr>
{% endfor %}
<tr class="totalColumn">
<td class=""></td>
<td class=""></td>
<td class=""></td>
<td class=""></td>
<td class="totalCol">Total: {{ asset.get_total_weight }}</td>
<td class=""></td>
</tr>
</tbody>
</table>
<p>Hope this is full ( {{ this_view }} )?</p>
<p class="text-center">{% include "sam/includes/backtosam.html" %}</p>
<!--
<script>
var total = 0;
$('#sum_table tr td.rowDataSd').each(function() {
total += parseInt($(this).text());
});
$('#sum_table td.totalCol').text("total: " + total);
</script>
-->
{% endblock %}
UPDATE - 3rd July 2016
from __future__ import unicode_literals
from django.db import models
from django.db.models import Sum, F, Count
from django.db.models import Max
from django.db.models import ExpressionWrapper
from django.db.models import Aggregate
from django.contrib.auth.models import User
class Asset(models.Model):
asset_name = models.CharField(max_length=30)
asset_quantity = models.IntegerField(default=0)
asset_weight = models.IntegerField(default=0)
total_assets_weight = models.IntegerField(default=0)
asset_owner = models.ForeignKey(
'AssetOwner',
on_delete=models.CASCADE,
) # This should be a Foreign Key Drop down of AssetOwners owner_name.
def by_item_weight(self):
"""This Method gives the total value of the asset. Weight * Qnty"""
total = self.asset_quantity * self.asset_weight
return total
def total_weight(self):
assets = Asset.objects.all().annotate(
total_weight=ExpressionWrapper(F('asset_quantity') * F('asset_weight'),
output_field=IntegerField()))
the_total = assets.aggregate(total=Sum('total_weight'))
return the_total
template
{% extends "personal/header.html" %}
{% block content %}
<h1 class='text-center'>This is the full asset list not split by owner</h1></br>
<table id="sum_table" class="well table table-striped text-center">
<thead>
<tr class="text-center titlerow">
<td class="text-center">Asset ID:</td>
<td class="text-center">Asset Name:</td>
<td class="text-center">Asset Quantity:</td>
<td class="text-center">Asset Weight / kg:</td>
<td class="text-center">Total Weight / kg:</td>
<td class="text-center">Asset Owner:</td>
</tr>
</thead>
<tbody>
<tr class="text-center">
{% for asset in object_list %}
<td><a href="/sam/assets/{{ asset.id }}">{{ asset.id }}</td>
<td>{{ asset.asset_name }}</td>
<td>{{ asset.asset_quantity }}</td>
<td>{{ asset.asset_weight }}</td>
<td class="rowDataSd">{{ asset.by_item_weight}}</td>
<td><a href="/sam/owners/">{{ asset.asset_owner }}</td>
</tr>
{% endfor %}
{% for total in object_list %}
<tr class="totalColumn">
<td class=""></td>
<td class=""></td>
<td class=""></td>
<td class=""></td>
<td class="totalCol">Total: {{ total.total_weight }}</td>
<td class=""></td>
</tr>
</tbody>
</table>
{% endfor %}
<p class="text-center">{% include "sam/includes/backtosam.html" %}</p>
{% endblock %}
You could use the ExpressionWrapper() (mostly Django 1.8+)
assets = Asset.objects.all().annotate(
total_weight=ExpressionWrapper(F('asset_quantity') * F('asset_weight'),
output_field=IntegerField() ))
That should give you the total weight for each object, i.e. quantity times weight.
Now, you should be able to get a sum from all the total_weight
s.
Edit: Now you can use Aggregate to get the total
assets.aggregate(total=Sum('total_weight'))
{'total': 1234.5678}
这篇关于Django总和的乘法值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!