I want to calculate the sum of two records but only inside the pagination.
class MacaListView(ListView):
model = Maca
paginate_by = 20
def get_context_data(self, *, object_list=None, **kwargs):
context = super(MacaListView, self).get_context_data(object_list=None, **kwargs)
maca = Maca.objects.all()
for i in range(0, maca.count()):
maca1 = Maca.objects.get(maca[i].id)
maca1.number = maca.number 1
maca1.save()
This will go to every single record and do the logic. I want the logic to be executed for only records in pagination (do calculate for only 20 records)
CodePudding user response:
Could you please try it? in here we get the current page number and slice queryset to get only needed elements.
class MacaListView(ListView):
model = Maca
paginate_by = 20
def get_context_data(self, *, object_list=None, **kwargs):
context = super(MacaListView, self).get_context_data(object_list=None, **kwargs)
page = int(self.request.GET.get('page', 1))
maca = Maca.objects.all()[(page-1)*self.paginate_by:self.paginate_by]
for i in range(0, maca.count()):
maca = Maca.objects.get(maca[i].id)
maca.number = maca.number 1
maca.save()