AttributeError - 类型对象“服务”没有属性“service_price”;
我正在尝试创建类似发票程序的东西,以创建发票并计算价格。
我仍在模型部分,我正在尝试计算单个发票中包含的所有服务并在 Invoices.subtotal 中更新它们。
我的问题是我无法将 Services.service_price 的汇总值传递给 Invoices.subtotal。
当我单击[保存并继续编辑]时,我看到:
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
这里是一些代码:
invoices/models.py
1 from django.db import models
2
3 from invmaster.general.models import Client
4 from invmaster.general.models import job_name
5 from invmaster.general.models import my_data
6
7 from decimal import *
8 import math
9
10 ### Invoices table
11 class Invoices (models.Model):
12 invoice_id = models.AutoField(primary_key=True)
13 client_name = models.ForeignKey(Client)
14 date_created = models.DateField()
15 subtotal = models.DecimalField('Precio sin IVA:(euros)',max_digits=6, decimal_places=2)
16 total = models.DecimalField('Precio Final:(euros)',max_digits=6, decimal_places=2, blank=True, null=True)
17 paid = models.BooleanField()
18 class Meta:
19 db_table = u'invInvoices'
20
21 def save(self, *args, **kwargs):
22 #self.subtotal = int(Services.service_price)
23 f = my_data()
24 self.subtotal = Services.service_price
25 self.total = self.subtotal * ((Decimal(f.IVA)/100)+1)
26 super(Invoices, self).save(*args, **kwargs)
27
28
29
30 ### Services table
31 class Services (models.Model):
32 JOB_CUANTITY = ( (u'H',u'Horas'),
33 (u'U',u'Unidades'),
34 )
35 invoice_id = models.ForeignKey(Invoices)
36 service_type = models.CharField('Tipo',max_length=1, choices=JOB_CUANTITY)
37 service_cuantity = models.IntegerField('Cantidad/Horas', max_length=2)
38 service_name = models.ForeignKey(job_name)
39 service_unit_price = models.DecimalField('Precio por unidad (euros):',max_digits=6, decimal_places=2,blank=True, null=True)
40 service_price = models.DecimalField('Precio (euros):',max_digits=6, decimal_places=2, blank=True, null=True)
41
42 class Meta:
43 db_table = u'invServices'
44 def __unicode__(self):
45 return u'%s' % (self.service_name)
46 def save(self, *args, **kwargs):
47 self.service_price = self.service_cuantity * self.service_unit_price
48 super(Services, self).save(*args, **kwargs)
其他文件: General/models.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
I'm trying to create something like an invoice program, to create invoices and calculate prices.
I am still on the models part and I am trying to calculate all the services includes in a single invoice and update them in Invoices.subtotal.
My problem is that I cannot pass the summary value of Services.service_price to Invoices.subtotal.
When I click [Save and Continue editing] I see this:
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
Here is some code:
invoices/models.py
1 from django.db import models
2
3 from invmaster.general.models import Client
4 from invmaster.general.models import job_name
5 from invmaster.general.models import my_data
6
7 from decimal import *
8 import math
9
10 ### Invoices table
11 class Invoices (models.Model):
12 invoice_id = models.AutoField(primary_key=True)
13 client_name = models.ForeignKey(Client)
14 date_created = models.DateField()
15 subtotal = models.DecimalField('Precio sin IVA:(euros)',max_digits=6, decimal_places=2)
16 total = models.DecimalField('Precio Final:(euros)',max_digits=6, decimal_places=2, blank=True, null=True)
17 paid = models.BooleanField()
18 class Meta:
19 db_table = u'invInvoices'
20
21 def save(self, *args, **kwargs):
22 #self.subtotal = int(Services.service_price)
23 f = my_data()
24 self.subtotal = Services.service_price
25 self.total = self.subtotal * ((Decimal(f.IVA)/100)+1)
26 super(Invoices, self).save(*args, **kwargs)
27
28
29
30 ### Services table
31 class Services (models.Model):
32 JOB_CUANTITY = ( (u'H',u'Horas'),
33 (u'U',u'Unidades'),
34 )
35 invoice_id = models.ForeignKey(Invoices)
36 service_type = models.CharField('Tipo',max_length=1, choices=JOB_CUANTITY)
37 service_cuantity = models.IntegerField('Cantidad/Horas', max_length=2)
38 service_name = models.ForeignKey(job_name)
39 service_unit_price = models.DecimalField('Precio por unidad (euros):',max_digits=6, decimal_places=2,blank=True, null=True)
40 service_price = models.DecimalField('Precio (euros):',max_digits=6, decimal_places=2, blank=True, null=True)
41
42 class Meta:
43 db_table = u'invServices'
44 def __unicode__(self):
45 return u'%s' % (self.service_name)
46 def save(self, *args, **kwargs):
47 self.service_price = self.service_cuantity * self.service_unit_price
48 super(Services, self).save(*args, **kwargs)
other files:
general/models.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)
Sorry for my English and for the question if it is stupid (I'm not a programmer, I'm a sysadmin, and I'm new in python/django :-) )
Thanks in advance
Cheers
UPDATE:
files:
linadmin.homeunix.net/models.txt
linadmin.homeunix.net/admin.txt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在任何地方都没有得到汇总值。
Services.service_price
在此上下文中没有任何意义 - 它是在类级别对模型字段本身的引用,而不是对其任何特定实例的值的引用。您需要一些代码来计算实际值。请记住,您有一个从“服务”到“发票”的外键,这意味着每个发票可以有许多服务。因此,大概您想要的是与此发票相关的所有服务的总价值。您可以使用聚合来解决此问题:
它对数据库中的所有相关服务执行
SUM
查询。You haven't got a summary value anywhere.
Services.service_price
makes no sense in this context - it's a reference to the model field itself, at the class level, rather than to the value of any particular instance of it.You need some code to calculate the actual value. Bear in mind that you have a ForeignKey from Services to Invoices, which means that each invoice can have many services. So presumably what you want is the total value of all services related to this invoice. You can work this out using an aggregation:
which does a
SUM
query against the database for all related services.