属性错误 - 类型对象'Services'没有属性'service_price'

2024-06-12 08:29:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建类似发票程序的东西,创建发票并计算价格。在

我仍然在模型的一部分,我试图计算所有的服务包括在一个单一的发票和更新他们发票.小计. 在

我的问题是我不能传递服务.服务价格到发票.小计. 在

当我单击[保存并继续编辑]时,我看到:

AttributeError at /admin/invoices/invoices/1/

type object 'Services' has no attribute 'service_price'

Request Method:  POST
Request URL:  http://192.168.1.3/invmaster/admin/invoices/invoices/1/
Django Version:  1.2.1
Exception Type:  AttributeError
Exception Value:  

type object 'Services' has no attribute 'service_price'

Exception Location:  /opt/invmaster/invoices/models.py in save, line 24
Python Executable:  /usr/bin/python
Python Version:  2.6.5

这里有一些代码:

发票/模型.py在

^{pr2}$

其他文件: 总则/模型.py在

### General table
  8 class my_data (models.Model):
...
 16         IVA = models.IntegerField(max_length=2, default='18') ### IVA = VAT # 18 = 18%
...
 26 ### Clients table
 27 class Client (models.Model):
 28         client_id = models.AutoField(primary_key=True)
 29         client_name = models.CharField(max_length=45, unique=True)
...
### Jobs
 58 class job_name (models.Model):
 59         job_type_id = models.AutoField(primary_key=True)
 60         job_name = models.CharField('Servicio/Producto',max_length=60, unique=True)
 61         job_price = models.DecimalField('Precio sin IVA (euros:)', max_digits=6, decimal_places=2)
 62 
 63         class Meta:
 64                 db_table = u'genJobNames'
 65 
 66         def __unicode__(self):
 67                 return u'%s (%s)' % (self.job_name, self.job_price)

抱歉我的英语和我的问题,如果它是愚蠢的(我不是一个程序员,我是一个系统管理员,我是一个新的python/django:-)

提前谢谢

干杯

更新:

文件:

linadmin.homeunix.net/models.txt
linadmin.homeunix.net/admin.txt

Tags: namepy模型trueadminmodelstypetable
1条回答
网友
1楼 · 发布于 2024-06-12 08:29:28

你在任何地方都没有摘要值。Services.service_price在这个上下文中没有意义-它是对模型字段本身的引用,在类级别上,而不是对它的任何特定实例的值的引用。在

你需要一些代码来计算实际值。请记住,您有一个ForeignKey from Services to Invoices,这意味着每个发票可以有许多服务。所以大概你想要的是与此发票相关的所有服务的总价值。您可以使用聚合来解决这个问题:

from django.db.models import Sum
service_sum = self.services_set.aggregate(Sum('service_price'))
self.subtotal = service_sum['service_price__sum']

它对数据库执行SUM查询,以获得所有相关服务。在

相关问题 更多 >