Suppose that we have page like dashboard,
More than one type object is listed:
def dashboard(request):
campaigns = Campaign.objects.all()
products = Product.objects.filter(campaign__name__isnull=False)
comments = CustomerComment.objects.filter(selected=True)
context={
'campaigns': campaigns,
'products' : products,
'comments': comments
}
return render(request, 'dashboard.html', context)
It will be a single page and the number of objects to be displayed on the page can be determined.
Which one is better solution?
Limiting querysets
campaigns = Campaign.objects.all()[:6]
products = Product.objects.filter(campaign__name__isnull=False)[:9]
comments = CustomerComment.objects.filter(selected=True)[:5]
or Paginator
campaign_paginator = Paginator(campaigns, 6)
page_number = 1
campaign_page_obj = campaign_paginator.get_page(page_number)
product_paginator = Paginator(products,9)
product_page_obj = product_paginator.get_page(page_number)
comment_paginator = Paginator(comments, 5)
comment_page_obj = comment_paginator.get_page(page_number)
Which approach is better especially in terms of performance and optimization?