Search code examples
djangopaginatordjango-pagination

Due to a loop before pagination Django paginator takes too much time. How can I solve this problem?


I was asked to map the object customer. I was trying to do the pagination before the loop but I don't know how to do it, because I think that you need to pass all the data to the paginator when creating.

This is my view

I think the problem is that when I call the function "get_customer_view_data" it runs the loop inside this function and I believe this happens everytime I change page on the paginator, causing the delay

class CustomersView(AdminStaffRequiredMixin, TemplateView):
    template_name = 'customers/tables.html'

    def get(self, request, activeCustumers, *args, **kwargs):
        controller = CustomerViewController()

        date1 = request.GET.get('date1', 1)
        date2 = request.GET.get('date2', 1)
        customer_view_data = controller.get_customer_view_data(activeCustumers, date1, date2)

        page = request.GET.get('page', 1)

        paginator = Paginator(customer_view_data, 10)
        try:
            customers_data = paginator.page(page)
        except PageNotAnInteger:
            customers_data = paginator.page(1)
        except EmptyPage:
            customers_data = paginator.page(paginator.num_pages)

        context = {'object_list': customers_data, 'num': len(customer_view_data)}

        return render(request, self.template_name, context)

And this is my controller where I map the data:

class CustomerViewController(object):


    def get_customer_view_data(self, get_active_custumers,date1,date2):
        data = []
        dates = self.set_dates(date1, date2)
        if get_active_custumers == 1:
            obj = Organization.objects.filter(organizationmainapp__application__appinfoforstore__status=2,
                                              deleted=False,
                                              status=True, to_deleted=False)
        else:
            obj = Organization.objects.all()

        for o in obj:
            customer_view_data = Customer()

            customer_view_data.Organization_id = o.id
            customer_view_data.Organization_name = o.name

            try:
                customer_view_data.monthly_price_plan = o.organizationmainapp.application.applicationselectedplan.price_plan.monthly_price
            except Exception as e:
                print(e)
            try:
                if o.organizationmainapp.application.applicationselectedplan.price_plan.transaction_percent_price is not None:
                    customer_view_data.commission = o.organizationmainapp.application.applicationselectedplan.price_plan.transaction_percent_price
                else:
                    customer_view_data.commission = o.organizationmainapp.application.applicationselectedplan.price_plan.transaction_fixed_price
            except Exception as e:
                print(e)

            try:
                customer_view_data.App_name = o.organizationmainapp.application.appinfoforstore.store_name
            except Exception as e:
                print(e)

            try:
                customer_view_data.plan = o.organizationmainapp.application.applicationselectedplan.plan_name_stamp
            except Exception as e:
                print(e)

            try:
                customer_view_data.Total_last_Month_sales = self.get_total_sales(o.id,dates['start_date'],dates['end_date'])['total']
            except Exception as e:
                print(e)

            try:
                customer_view_data.Total_last_Month_sales_without_shipping_cost = \
                    self.get_total_sales(o.id, dates['start_date'], dates['end_date'])['total_without_shipping_cost']
            except Exception as e:
                print(e)
            try:
                customer_view_data.Main_mail = o.organizationmainapp.application.user.email
            except Exception as e:
                print(e)
            data.append(customer_view_data)

        return data

Solution

  • Sorry

    This is how I solved the problem. First I create the paginator and then I map the data. For pagination purposes I also pass the "organization_data" to the context. This is not the actual code but is something like this:

    My view

    class CustomersView(AdminStaffRequiredMixin, TemplateView):
    template_name = 'customers/tables.html'
    
    def get(self, request, *args, **kwargs):
        controller = CustomerViewController()
        organizations = controller.get_customer_view_data()
        
        page = request.GET.get('page', 1)
    
        paginator = Paginator(organizations, 15)
        try:
            organization_data = paginator.page(page)
        except PageNotAnInteger:
            organization_data = paginator.page(1)
        except EmptyPage:
            organization_data = paginator.page(paginator.num_pages)
    
        customers_data = controller.map_view_data(organization_data.object_list)
        # loop over each organization and return customers data
        context = {'object_list': customers_data, 'organization': organization_data}
    
        return render(request, self.template_name, context)