- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试创建类似发票程序的程序,以创建发票和计算价格。
我仍在模型部分,我正在尝试计算单个发票中包含的所有服务,并在 Invoices.subtotal 中更新它们。
我的问题是我无法将 Services.service_price 的汇总值传递给 Invoices.subtotal。
当我点击 [Save and Continue editing] 时,我看到了这个:
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
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)
其他文件:通用/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
最佳答案
您在任何地方都没有汇总值。 Services.service_price
在此上下文中没有意义 - 它是在类级别对模型字段本身的引用,而不是对它的任何特定实例的值的引用。
您需要一些代码来计算实际值。请记住,您有一个从服务到发票的外键,这意味着每张发票可以有许多服务。所以大概你想要的是与这张发票相关的所有服务的总值(value)。您可以使用聚合来解决这个问题:
from django.db.models import Sum
service_sum = self.services_set.aggregate(Sum('service_price'))
self.subtotal = service_sum['service_price__sum']
它针对所有相关服务的数据库执行 SUM
查询。
关于python - AttributeError - 类型对象 'Services' 没有属性 'service_price',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3422342/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!