简体   繁体   English

Django 错误:类型 object 'Invoice_Line' 没有属性 'objects'

[英]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.

相关问题 在invoice_line中扩展on_change方法时出错 - Error when extend on_change method in invoice_line django类型的对象'PoliceDefenceJobs'没有属性'objects' - django type object 'PoliceDefenceJobs' has no attribute 'objects' 在 Django 中创建用户时出现以下错误:类型对象“用户”没有属性“对象” - Get following error when creating user in Django: type object 'User' has no attribute 'objects' “属性错误/功能”object 在 Django 中没有属性“对象” - 'Attribute Error/ function' object has no attribute 'objects' in Django Python / DJango属性错误:模型对象没有属性对象 - Python/DJango Attribute error : Model object has not attribute objects 类型对象“购物车”在Django网站中没有属性“对象” - type object 'Cart' has no attribute 'objects' in django website Django AttributeError: type object 'Rooms' 没有属性 'objects' - Django AttributeError: type object 'Rooms' has no attribute 'objects' DJANGO自定义用户模型错误“ str”对象没有属性“ objects” - DJANGO custom user model error 'str' object has no attribute 'objects' 表单提交后,类型对象在django中没有属性错误 - type object has no attribute error in django after form submission Django RESTful API 错误“类型对象‘用户’没有属性‘_meta’” - Django RESTful API error "type object 'User' has no attribute '_meta'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM