【问题标题】:How to limit query within Django Admin.TabularInline with large scale data如何使用大规模数据限制 Django Admin.TabularInline 中的查询
【发布时间】:2019-10-09 20:44:02
【问题描述】:

我目前正在使用 admin.TabularInline 加载 50k 条数据记录,由于超时,页面无法加载。我使用的是 Django 1.9 版,升级不是一个选项。

我已经尝试覆盖 get_queryset 以仅返回 10 条记录。

class RemDetailInline(admin.TabularInline):
    model = RemDetail
    fields = ('rem_name', 'ben_name', 'payout_amount', 
              'payout_currency', 'status','type', 'date_created')
    readonly_fields = ('rem_name', 'ben_name', 'payout_amount', 
                       'payout_currency', 'status', 'type', 'date_created')
    extra = 0
    max_num = 0
    show_change_link = True
    ordering = ['-date_created',]

    def get_queryset(self, request):
        queryset = super(RemDetailInline, self).get_queryset(request)
        ids = queryset.order_by('-id').values('pk')[:10] # limit 10
        qs = RemDetailInline.objects.filter(pk__in=ids).order_by('-id')
        return qs

我希望输出返回 10 条记录,而不是由于大量(50k 条记录)导致页面超时

【问题讨论】:

    标签: django python-2.7 django-models django-forms django-admin


    【解决方案1】:

    你试过了吗?

    class RemDetailInline(admin.TabularInline):
        ...
    
        def get_queryset(self, request):
            return super(RemDetailInline, self).get_queryset(request).order_by('-id')[:10]

    【讨论】:

    • 我猜这可行,但它现在返回错误为python AssertionError: Cannot filter a query once a slice has been taken.
    猜你喜欢
    • 2015-09-26
    • 2020-10-30
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 2016-01-13
    相关资源
    最近更新 更多