通过models.py做加法的一个简单计算题
An issue making addition through models.py making a simple calculation
大家好,我在 Django 上练习,现在正在为一个主要项目示例开发一个简单的费用跟踪器:以 X 金额购买一辆可折叠汽车,并将所有其他费用相加得到一个总数。
我的操作主要是在我的model.py
models.py
from django.db import models
# Create your models here.
class Project(models.Model):
title = models.CharField(max_length=100)
amount = models.FloatField()
description = models.TextField(blank=True, null=True)
def __str__(self):
return self.title
class Expenses(models.Model):
project = models.ForeignKey(Project, related_name='expenses', on_delete=models.CASCADE)
title = models.CharField(max_length=100)
amount = models.FloatField()
def __str__(self):
return self.title
def get_total_project_amount(self):
return self.amount + self.project.amount
我想做的是将主要项目和所有费用加在一起,但我没有使用正确的方法,当我渲染结果时它呈现如下:
主要项目:50$
从模型表格中添加的费用:2(新)
费用:13(新)
费用:25(新)
总数为:52
总计:63
总计:75
我想渲染主要项目和所有费用以获得单个结果,比如 50+2+13+25
总计:修车总计 90 美元。
请懂加法lojic的朋友帮忙谢谢
.
您可以用以下方式总结费用:
class Project(models.Model):
# …
@property
def total_amount(self):
subtotal = self.expenses.<strong>aggregate(</strong>
subtotal=Sum('amount')
<strong>)</strong>['subtotal'] or 0
return <strong>self.amount + subtotal</strong>
因此,您可以确定 Project
对象 myproject
的总成本:
<em>myproject</em><strong>.total_amount</strong>
这将总结 amount
加上所有 相关的 Expenses
.
的总和
Note: normally a Django model is given a singular name, so Expense
instead of Expenses
.
Note: When you make calculations with money, it is better to use a DecimalField
[Django-doc],
not a FloatField
[Django-doc]. Floats can generate rounding errors when you perform calculations.
You can also use django-money
[GitHub] to specify a MoneyField
. This has also specifies a currency, and can convert money to a different
currency.
大家好,我在 Django 上练习,现在正在为一个主要项目示例开发一个简单的费用跟踪器:以 X 金额购买一辆可折叠汽车,并将所有其他费用相加得到一个总数。
我的操作主要是在我的model.py
models.py
from django.db import models
# Create your models here.
class Project(models.Model):
title = models.CharField(max_length=100)
amount = models.FloatField()
description = models.TextField(blank=True, null=True)
def __str__(self):
return self.title
class Expenses(models.Model):
project = models.ForeignKey(Project, related_name='expenses', on_delete=models.CASCADE)
title = models.CharField(max_length=100)
amount = models.FloatField()
def __str__(self):
return self.title
def get_total_project_amount(self):
return self.amount + self.project.amount
我想做的是将主要项目和所有费用加在一起,但我没有使用正确的方法,当我渲染结果时它呈现如下:
主要项目:50$ 从模型表格中添加的费用:2(新) 费用:13(新) 费用:25(新)
总数为:52 总计:63 总计:75
我想渲染主要项目和所有费用以获得单个结果,比如 50+2+13+25 总计:修车总计 90 美元。
请懂加法lojic的朋友帮忙谢谢
.
您可以用以下方式总结费用:
class Project(models.Model):
# …
@property
def total_amount(self):
subtotal = self.expenses.<strong>aggregate(</strong>
subtotal=Sum('amount')
<strong>)</strong>['subtotal'] or 0
return <strong>self.amount + subtotal</strong>
因此,您可以确定 Project
对象 myproject
的总成本:
<em>myproject</em><strong>.total_amount</strong>
这将总结 amount
加上所有 相关的 Expenses
.
Note: normally a Django model is given a singular name, so
Expense
instead of.Expenses
Note: When you make calculations with money, it is better to use a
DecimalField
[Django-doc], not aFloatField
[Django-doc]. Floats can generate rounding errors when you perform calculations. You can also usedjango-money
[GitHub] to specify aMoneyField
. This has also specifies a currency, and can convert money to a different currency.