我是python和django的新手,在我的model.py文件中执行数学函数时遇到了麻烦.
class Orders(models.Model):
...
total = models.DecimalField(
max_digits = 6,
decimal_places = 2,
null = True,
blank = True,
)
...
def shipping(self):
t = self.total
ship_rate = 0.12
return(t*ship_rate)
Run Code Online (Sandbox Code Playgroud)
当我在python shell中调用它时
dat = Orders.object.get(pk=12)
dat.shipping()
Run Code Online (Sandbox Code Playgroud)
然后我收到以下错误消息:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\xx\xx\models.py", line 613, in shipping
ship_rate = 0.12
TypeError: 'module' object is not callable
Run Code Online (Sandbox Code Playgroud)
谁能看到我做错了什么?
错误是因为你使用过
ship_rate = decimal(0.12)
Run Code Online (Sandbox Code Playgroud)
这应该是
ship_rate = decimal.Decimal(0.12)
Run Code Online (Sandbox Code Playgroud)
decimal是模块的名称.您无法调用模块,这就是错误消息所说的内容.你得到奇怪回溯的原因是模块源和内存中的代码不同步.创建回溯时,将使用该文件的当前版本,该版本可能不是实际运行的代码版本.始终重新加载您的网络服务器,以确保它使用的是最新版本的代码.
| 归档时间: |
|
| 查看次数: |
11445 次 |
| 最近记录: |