关于django admin的最慢查询

时间:2014-06-10 09:36:58

标签: django django-models django-admin

我需要从此查询中按条件删除订单。这是添加到projectG_coupondataid

的条件的默认顺序
SELECT
   projectG_coupondata.id,
   projectG_coupondata.vin_id,
   projectG_coupondata.unique_service_coupon,
   projectG_coupondata.valid_days,
   projectG_coupondata.valid_kms,
   projectG_coupondata.service_type,
   projectG_coupondata.sa_phone_number_id,
   projectG_coupondata.status,
   projectG_coupondata.closed_date,
   projectG_coupondata.mark_expired_on,
   projectG_coupondata.actual_service_date,
   projectG_coupondata.actual_kms,
   projectG_coupondata.last_reminder_date,
   projectG_coupondata.schedule_reminder_date,
   projectG_coupondata.order,
   projectG_coupondata.extended_date,
   projectG_productdata.id,
   projectG_productdata.vin,
   projectG_productdata.customer_phone_number_id,
   projectG_productdata.product_type_id,
   projectG_productdata.sap_customer_id,
   projectG_productdata.product_purchase_date,
   projectG_productdata.invoice_date,
   projectG_productdata.dealer_id_id,
   projectG_productdata.engine,
   projectG_productdata.customer_product_number,
   projectG_productdata.purchased_from,
   projectG_productdata.seller_email,
   projectG_productdata.seller_phone,
   projectG_productdata.warranty_yrs,
   projectG_productdata.insurance_yrs,
   projectG_productdata.invoice_loc,
   projectG_productdata.warranty_loc,
   projectG_productdata.insurance_loc,
   projectG_productdata.last_modified,
   projectG_productdata.created_on,
   projectG_productdata.isActive,
   projectG_productdata.order 
FROM
   projectG_coupondata 
INNER JOIN
   projectG_productdata 
      ON (
         projectG_coupondata.vin_id = projectG_productdata.id
      ) 
ORDER BY
   projectG_coupondata.id DESC LIMIT 20

1 个答案:

答案 0 :(得分:0)

此代码解决了问题:

def get_changelist(self, request, **kwargs):
    return CouponChangeList


from django.contrib.admin.views.main import ChangeList


class CouponChangeList(ChangeList):

    def get_query_set(self, request):
        qs = super(CouponChangeList, self).get_query_set(request)
        qs = qs.select_related('vin')  # Don't join on dealer or category
        return qs

    def get_ordering(self, request, queryset):
        return []

问题出在get_ordering函数中,如果未指定排序,则默认情况下将主键添加为订单键。