在 Django 中转换数据。希望能够将求和应用于小数字段
我有一个这样的模型。
#models.py
class Payment(models.Model):
unit_price = models.DecimalField(max_digits=12, decimal_places=2)
discount = models.DecimalField(max_digits=12, decimal_places=2)
payment_terms = models.CharField(max_length=80)
amount = models.DecimalField(max_digits=12, decimal_places=2)
VAT = models.DecimalField(max_digits=4, decimal_places=2)
def __unicode__(self):
return unicode(self.unit_price)
class Invoice(models.Model):
client = models.ForeignKey(Client)
payment = models.ForeignKey(Payment)
date = models.DateField()
invoice_no = models.CharField(max_length=16)
work_orders = models.ManyToManyField(Work_Order)
contract_info = models.ForeignKey(Contract_Info)
def __unicode__(self):
return self.invoice_no
我想关注的是付款,所以尝试忘记其他一切。这是我的观点.py。
#views.py
@login_required
def invoice_details(request, id=1):
invoices_list = Invoice.objects.filter(pk=id)
invoice = get_object_or_404(Invoice, pk=id)
client = invoices_list[0].client
work_orders = invoices_list[0].work_orders
payment = invoices_list[0].payment
return render_to_response(('invoice_details.html', locals()), {'work_orders': work_orders,'payment':payment,'client': client, 'invoice': invoice ,'invoices_list': invoices_list}, context_instance=RequestContext(request))
这是我的模板。
<h1>invoice_details.html<h1>
{{ payment.amount }}
该模板以十进制值显示付款金额。我想要做的是使用 {{ payment.amount}} 进行一些金额。假设我想乘以任何值
{{ payment.amount}} 假设 2.我该怎么做?
I have a model like this.
#models.py
class Payment(models.Model):
unit_price = models.DecimalField(max_digits=12, decimal_places=2)
discount = models.DecimalField(max_digits=12, decimal_places=2)
payment_terms = models.CharField(max_length=80)
amount = models.DecimalField(max_digits=12, decimal_places=2)
VAT = models.DecimalField(max_digits=4, decimal_places=2)
def __unicode__(self):
return unicode(self.unit_price)
class Invoice(models.Model):
client = models.ForeignKey(Client)
payment = models.ForeignKey(Payment)
date = models.DateField()
invoice_no = models.CharField(max_length=16)
work_orders = models.ManyToManyField(Work_Order)
contract_info = models.ForeignKey(Contract_Info)
def __unicode__(self):
return self.invoice_no
What I want to focus is payment so try forgetting everything else. Here is my views.py.
#views.py
@login_required
def invoice_details(request, id=1):
invoices_list = Invoice.objects.filter(pk=id)
invoice = get_object_or_404(Invoice, pk=id)
client = invoices_list[0].client
work_orders = invoices_list[0].work_orders
payment = invoices_list[0].payment
return render_to_response(('invoice_details.html', locals()), {'work_orders': work_orders,'payment':payment,'client': client, 'invoice': invoice ,'invoices_list': invoices_list}, context_instance=RequestContext(request))
And here is my template.
<h1>invoice_details.html<h1>
{{ payment.amount }}
The template displays the payment amount in a decimal value. What I want to be able to do is some sums with {{payment.amount}}. Suppose if I wanted to multiply whatever the value
{{payment.amount}} by let say 2. How would I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会在视图中执行此操作,
但如果出于某种原因您不能或不想这样做,您可以通过使用模板标签在模板中实现类似的功能。我不认为有一个乘法标签,但有一个加法:
只需将 2 添加到您的付款金额,类似地
会将显示的值加倍。添加可能并不完全是您想要做的,因此您可以尝试制作自己的模板标记来使用乘法或您想要的任何函数,请参阅 http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
I would do this in the view,
But if there's some reason you cannot or do not want to, you can acheive something similar in the template by using template tags. I do not think there is a multiply tag, but there is an add:
Would simply add 2 to your payment amount, similarly
Would double the value displayed. Adding is likely not exactly what you wanted to do, so you can attempt to make your own template tag to use multiply or whatever function you'd like, see http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
我不知道为什么您需要在模板内而不是在视图中完成此操作,但这是我的解决方案。有一个关于 Django 模板中的算术的长讨论,关于不支持您所描述的具体用例的此功能。
如果您仍想继续,这可能会有所帮助。在 Django 文档中,有一个
|add
内置模板过滤器 这似乎是算术的原始形式。不幸的是,没有|multiply
过滤器。您可能可以在 Django 安装下的template/defaultfilters.py
中找到该过滤器。复制它,更改您需要的内容,使其成为乘积而不是总和。完成后,您可能希望将其提交到 Django 项目,以使其成为未来版本中的内置组件。
I don't know why you need this to be done inside the template and not in the view, but here is my go at a solution. There is a long discussion about arithmetic in Django templates about not supporting this feature for specifically the use case you described.
If you still want to proceed, this might help. In the Django docs, there is an
|add
built-in template filter that seems to be a primitive form of arithmetic. Unfortunately, there is no|multiply
filter. You can probably find the filter in thetemplate/defaultfilters.py
under your Django installation.Copy it, change what you need to make it a product instead of a sum. When you're done, you might want to submit it to the Django project to make it a buil-tin in future releases.
答案如下:
在模板中放置此自定义标签(我认为它是什么)来表示值。
Here is the answer:
In a template put this custom tag (I think that what it is) to represent the value.