[英]Django Error: type object 'Invoice_Line' has no attribute 'objects'
I defined a method in admin.py
to add up after-tax amounts:我在admin.py
中定义了一个方法来计算税后金额:
def _grand_total(self, object_id):
line_amounts = Invoice_Line.objects.filter(appointment_ref=object_id).values_list('total_after_tax', flat=True)
return reduce((lambda x,y: x + y), line_amounts)
The ORM query defined in this line: Invoice_Line.objects.filter(appointment_ref=object_id).values_list('total_after_tax', flat=True)
此行中定义的 ORM 查询: Invoice_Line.objects.filter(appointment_ref=object_id).values_list('total_after_tax', flat=True)
generates the error below:生成以下错误:
type object 'Invoice_Line' has no attribute 'objects'类型 object 'Invoice_Line' 没有属性 'objects'
Models code shown below:型号代码如下图:
models.py:模型.py:
class Appointment(models.Model):
create_date = models.DateField(auto_now_add=True, db_index=True)
appointment_ref = models.AutoField(primary_key=True)
appointment_date = models.DateField()
class Invoice_Line (models.Model):
appointment_ref = models.ForeignKey(Appointment, on_delete=models.CASCADE)
inv_line_date = models.DateTimeField(auto_now_add=True)
item_price = models.DecimalField(max_digits=7, decimal_places=2, default=00000.00, verbose_name='Service Amount')
tip_amount = models.DecimalField(max_digits=7, decimal_places=2, default=00000.00, verbose_name='Tip Amount')
item_quantity = models.PositiveSmallIntegerField(blank=False, null=True, default=0, verbose_name='Service Quantity')
total_before_tax = models.DecimalField(max_digits=7, decimal_places=2, default=00000.00, verbose_name='Before Tax')
stax_rate = models.DecimalField(max_digits=5, decimal_places=4, blank=True, null=True, verbose_name='Sales Tax Rate')
stax_amount = models.DecimalField(max_digits=7, decimal_places=2, default=00000.00, verbose_name='Sales Tax Amount')
total_after_tax = models.DecimalField(max_digits=7, decimal_places=2, default=00000.00, verbose_name='Total After Tax')
I run the same query in the shell/interpreter, and it works just fine.我在 shell/解释器中运行相同的查询,它工作得很好。 Can I get assistance in fixing this error?我可以在修复此错误方面获得帮助吗?
Any help will be appreciated!任何帮助将不胜感激!
Thanks!谢谢!
Fixed the issue:修复了问题:
As I mentioned in my question, I had defined the method below in admin.py:正如我在问题中提到的,我在 admin.py 中定义了以下方法:
def _grand_total(self, object_id):
line_amounts = Invoice_Line.objects.filter(appointment_ref=object_id).values_list('total_after_tax', flat=True)
return reduce((lambda x,y: x + y), line_amounts)
And I had also registered "Invoice_Line" model in admin.py as shown below:我还在 admin.py 中注册了“Invoice_Line”model,如下所示:
@admin.register (Invoice_Line)
class Invoice_Line(admin.ModelAdmin):
list_display = ('__str__','inv_line_date','total_after_tax','appointment_ref')
readonly_fields = ('item_price','total_before_tax','stax_rate','stax_amount','total_after_tax')
The problem was that you can't name the class you use to register a model in admin.py exactly the same as how you named the model in models.py.问题是您无法命名 class 用于在 admin.py 中注册 model 与您在 models.py 中命名 model 的方式完全相同。
In other words you can't do this:换句话说,你不能这样做:
models.py:模型.py:
class Invoice_Line (models.Model):
......
......
admin.py:管理员.py:
@admin.register (Invoice_Line)
class Invoice_Line (admin.ModelAdmin):
.......
.......
Once I changed the name of the class in admin.py to " class InvoiceLineAdmin (admin.ModelAdmin): " the ORM query originating the error I was trying to fix worked just fine.一旦我将 admin.py 中的 class 的名称更改为“ class InvoiceLineAdmin (admin.ModelAdmin): ” ORM 查询就可以正常工作,因为我试图修复错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.