简体   繁体   English

如何在 Django Admin.py 中按顺序显示 OrderItem

[英]How to show OrderItem in Order in Django Admin.py

I am trying to show the orderitemsadmin in the orderadmin using TabularInline but I keep getting the following error.我试图展现orderitemsadminorderadmin使用TabularInline但我不断收到以下错误。

AttributeError: 'OrderItemAdmin' object has no attribute 'urls'

This is a Django e-commerce project and I am trying to facilitating the admin viewing the orders and their items.这是一个 Django 电子商务项目,我试图帮助管理员查看订单及其项目。

I think the reason for the error is that I can't set a foreign relation with OrderItem and Order as the sequence is: Orderitem which has a foreign relation with Item, and Order which has a manytomany relation with OrderItem, if the switched their location or sequence it will mess with the ManytoMany relation.我认为错误的原因是我无法设置与 OrderItem 和 Order 的外部关系,因为顺序是:Orderitem 与 Item 有外部关系,Order 与 OrderItem 有很多关系,如果切换了它们的位置或序列它会弄乱多对多关系。

Here is the admin.py这是admin.py

class OrderItemAdmin(admin.TabularInline):
    list_display = ['item', 'quantity', 'ordered']
    model = OrderItem

class OrderAdmin(admin.ModelAdmin):
    list_display = ['user', 'ordered', 'ordered_date', 'coupon', 'payment', 'shipping_address', 'status',
                    'refund_requested', 'refund_granted', 'ref_code']
    inlines = [
        OrderItemAdmin,
    ]
admin.site.register(Order, OrderAdmin)

Here is the models.py这是models.py

class OrderItem(models.Model):
    item = models.ForeignKey(Item, on_delete=models.CASCADE)

class Order(models.Model):
    items = models.ManyToManyField(OrderItem)

You need to remove你需要删除

admin.site.register(OrderItem, OrderItemAdmin) admin.site.register(OrderItem, OrderItemAdmin)

this line.这条线。 An inline can only be used in a modelAdmin that is why you get that error.内联只能在 modelAdmin 中使用,这就是您收到该错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM