简体   繁体   中英

Django Select Related in Template Paginated Search Results to Reduce Queries?

I have a model (products) which is actually containing a number of many-to-many fields.

Ideally I am trying to reduce queries to the number of products themselves -- 25, but considering the depth of foreignkeys and m2ms, I understand this might not be possible. But still I'm shooting for best results.

Here is my view:

def index(request):
    products = Product.objects.select_related()
    paginator = Paginator(products, 25) 
    page = request.GET.get('page')

    try:
        products = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        products = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        products = paginator.page(paginator.num_pages)

    print (len(connection.queries))
    return render(request, 'editor-index.html', {
        'products': products, 
        'connection':connection,
        'csrf':csrf(request)
        })

How can I reduce the queries in this? It would be best to prefetch all data related to the product. Is such a thing possible?

select_related only works for foreign keys. For many to many fields you can use prefetch_related . I think that with prefect related you have to specify the related models you want to fetch, and that you can't call it with no arguments like select_related .

Note that you shouldn't need 25 queries for 25 products. It should be possible to reduce it to one query for the products queryset, one additional query for each prefetch_related argument, and (because you are using a paginator) one for the total number of objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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