我正在尝试将在一个类中计算的属性转换为另一个类。我被卡住了......
在“奖励”课程中,我需要从“pledge_level”中减去“deductible”。在“承诺”课程中,我需要从“金额”中减去“not_taxable”以返回“decuctible_total”。
在我的模板中访问它时可以正常工作:
(${{ reward.deductible}} is tax deductible or ${{ reward.not_taxable }} is not tax deductible)
在服务器上运行时,我收到内部服务器错误:[Django] ERROR(EXTERNAL IP):内部服务器错误:/ projects / fund / billing / 1000
这是追溯:
Internal Server Error: /projects/fund/billing/1000/
Traceback (most recent call last):
File "/var/venv/website/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/sites/proj/payments/views.py", line 98, in pledge_billing
html = template.render(c)
File "/var/venv/website/lib/python2.7/site-packages/django/template/backends/django.py", line 74, in render
return self.template.render(context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/base.py", line 209, in render
return self._render(context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/base.py", line 201, in _render
return self.nodelist.render(context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/base.py", line 903, in render
bit = self.render_node(node, context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/base.py", line 917, in render_node
return node.render(context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/loader_tags.py", line 135, in render
return compiled_parent._render(context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/base.py", line 201, in _render
return self.nodelist.render(context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/base.py", line 903, in render
bit = self.render_node(node, context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/base.py", line 917, in render_node
return node.render(context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/loader_tags.py", line 65, in render
result = block.nodelist.render(context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/base.py", line 903, in render
bit = self.render_node(node, context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/base.py", line 917, in render_node
return node.render(context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/base.py", line 957, in render
output = self.filter_expression.resolve(context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/base.py", line 647, in resolve
obj = self.var.resolve(context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/base.py", line 787, in resolve
value = self._resolve_lookup(context)
File "/var/venv/website/lib/python2.7/site-packages/django/template/base.py", line 825, in _resolve_lookup
current = getattr(current, bit)
File "/var/sites/proj/projects/models.py", line 81, in deductible_total
return self.get_deductible_total()
File "/var/sites/proj/projects/models.py", line 77, in get_deductible_total
return (self.amount - self.not_taxable)
AttributeError: 'Pledge' object has no attribute 'not_taxable'
models.py
class Pledge(models.Model):
project = models.ForeignKey('Project')
amount = models.DecimalField(decimal_places=2,max_digits=10,
default=D('0.00'))
reward = models.ForeignKey('Reward',blank=True,null=True,
on_delete=models.SET_NULL)
objects = PledgeManager()
def get_deductible_total(self):
return (self.amount - self.not_taxable)
@property
def deductible_total(self):
return self.get_deductible_total()
class Reward(models.Model):
project = models.ForeignKey('Project')
title = models.CharField(max_length=255)
pledge_level = models.DecimalField(decimal_places=2, max_digits=10)
deductible = models.DecimalField(decimal_places=2, max_digits=10)
description = models.TextField(blank=True)
def get_not_taxable(self):
return (self.pledge_level - self.deductible)
@property
def not_taxable(self):
return self.get_not_taxable()
因为我正在编辑项目应用程序中的models.py文件,所以我在项目上进行了迁移。也许支付应用还需要由于项目的变化而在其上运行迁移?
非常感谢您的帮助!
答案 0 :(得分:1)
更改方法如下:
def get_deductible_total(self):
return (self.amount - self.reward.not_taxable)
BTW通常在分离属性和方法方面没有任何意义,你也可以在属性中进行计算。你可以拥有
@property
def deductible_total(self):
return (self.amount - self.not_taxable)
但有些理由将它们分开:
1)属性不会引发错误,所以如果你在需要异常的其他地方调用它,那么将它们分开是有意义的。
2)如果由于某种原因你需要一个返回结果的方法(或一个函数,基本上是一个可调用的),那么你还需要将它们分开。可能不是这种情况,可以使用lambda
来对抗
仅供参考。