Reputation: 11152
I wrote a view with pagination and multiple querysets. How ever, as I'm using namedtupled, sorted() and chain() method before actually using pagination, I was asking myself if my queries could be interpreted before I actually use Paginator.
In that case, my code would be dummy: pagination would seemed to work as expected but performances would be awful. (I can't check it now since I have a powerful computer and few data).
Could you tell me in that case if everything is ok and queries are NOT executed before pagination occurs?
scan_evts = [ScanEventTuple(
evt_id=e.pk,
date=e.created) \
for e in ScanEvent.get_scan_events(shop=shop_to_check) \
.order_by('-created')]
userewards_evts = [UseRewardEventTuple(
evt_id=e.pk,
date=e.created) \
for e in UseRewardEvent.get_usereward_events(shop=shop_to_check) \
.order_by('-created')]
redeemdeals_evts = [RedeemDealsEventTuple(
dr_id=e.pk,
date=e.redeem_date) \
for e in DealRecipients.get_redeem_events(shop=shop_to_check) \
.order_by('-redeem_date')]
event_list = sorted(
chain(scan_evts, userewards_evts, redeemdeals_evts),
key=attrgetter('date'),
reverse=True)
# Pagination
paginator = Paginator(event_list, 20)
page = request.GET.get('page')
try:
all_events = paginator.page(page)
except PageNotAnInteger: # Mauvaise parametre
all_events = paginator.page(1)
except EmptyPage: # Page out of range
all_events = paginator.page(paginator.num_pages)
Upvotes: 0
Views: 172
Reputation: 37319
Your query sets are not lazily evaluated here. As soon as you iterate over them (as for example in for e in ScanEvent.get...
), the full query set is evaluated. See:
https://docs.djangoproject.com/en/1.8/ref/models/querysets/#when-querysets-are-evaluated
I do not think you're going to be able to paginate over three different query sets without evaluating them.
Upvotes: 3