Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
Make calculations for records inside pagination, django

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)

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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()
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!