简体   繁体   English

如何为订单模型的 Django Admin 分页

[英]How to paginate Django Admin for Order Model

In my E-commerce Project, Everytime I click on the orders model in Django Admin, it takes a while to load, so I thought the reason might be because of the many orders in the page which is not paginated.在我的电商项目中,每次在Django Admin中点击订单模型,加载需要一段时间,所以我认为可能是因为页面中的许多订单没有分页。

So my question is how to paginate the order.model in Django Admin, second is this the reason why it is the only page that is taking a while to load or might there be another reason that I should be thinking of?所以我的问题是如何在 Django Admin 中对 order.model 进行分页,其次是这就是为什么它是唯一需要一段时间才能加载的页面的原因,还是我应该考虑的另一个原因?

Here is the models.py这是models.py

class Order(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             on_delete=models.CASCADE)
    ref_code = models.CharField(max_length=20, unique=True, blank=True, null=True)
    items = models.ManyToManyField(OrderItem)
---------------------------------------

Here is the admin.py这是admin.py

class OrderAdmin(admin.ModelAdmin):
    list_display = ['id', 'user', 'ordered', 'ordered_date', 'coupon', 'payment', 'shipping_address', 'status',
                    'refund_requested', 'refund_granted', 'ref_code']
    list_display_links = [
        'id',
    ]
    list_editable = ['status']
    list_filter = ['ordered', 'ordered_date', 'refund_requested', 'refund_granted', 'status']
    search_fields = [
        'user__username',
        'ref_code'
    ]
    actions = [make_refund_accepted]

There is already pagination in default and default page size is 100默认已经有分页,默认页面大小为 100

You can reduce/change it to any number eg: 20 by list_per_page您可以将其减少/更改为任意数字,例如:20 by list_per_page

class OrderAdmin(admin.ModelAdmin):
    list_display = ['id', 'user', 'ordered', 'ordered_date', 'coupon', 'payment', 'shipping_address', 'status',
                    'refund_requested', 'refund_granted', 'ref_code']
    list_display_links = [
        'id',
    ]
    list_editable = ['status']
    list_filter = ['ordered', 'ordered_date', 'refund_requested', 'refund_granted', 'status']
    search_fields = [
        'user__username',
        'ref_code'
    ]
    actions = [make_refund_accepted]
    list_per_page = 20

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

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