'Affect total sum with start date and end date

My views:

class CentreRevenue(ListAPIView):
permission_classes = [IsAuthenticated, ]
pagination_class = CustomPagination
serializer_class = serializers.CentreRevenueSerializer

def get_queryset(self):
    self.pagination_class.page_size = page_size
    id = self.request.query_params.get("id")
    start_date = self.request.query_params.get("start_date")
    end_date = self.request.query_params.get("end_date")
    data = center_models.Centers.objects.filter(center_type__in=['collection_center', 'direct_client'], id__isnull=False)

    if id:
        data = data.filter(id=id)

    # if start_date and end_date:
    #     data = data.filter(created_at__date__range=[start_date, end_date])
        
    return data

#Serializer

class CentreRevenueSerializer(serializers.BaseSerializer):
class Meta:
    model = package_models.CollectionCenterLedger

def to_representation(self, instance):
    tbc = booking_models.Booking.objects.filter(center=instance, center__isnull=False).values_list('id').count()
    
    amount = sum(
        package_models.CollectionCenterLedger.objects.filter(
            package__isnull=False,
            center=instance,
            ledger_type='debit' if instance.type == 'prepaid' else 'credit'
            ).values_list('amount', flat=True)
        )
    remove_test_billing_client_billing = sum(
        package_models.CollectionCenterLedger.objects.filter(
            package__isnull=False,
            ledger_type='credit' if instance.type == 'prepaid' else 'debit',
            center=instance,
            ).values_list('amount', flat=True)
    )

    add_test_billing = sum(
        package_models.CollectionCenterLedger.objects.filter(
            package__isnull=False,
            ledger_type='debit' if instance.type == 'prepaid' else 'credit',center=instance
            ).values_list('rcl_share', flat=True)
    )
    remove_test_billing = sum(
        package_models.CollectionCenterLedger.objects.filter(
            package__isnull=False,
            ledger_type='credit' if instance.type == 'prepaid' else 'debit',
            center=instance
            ).values_list('rcl_share', flat=True)
    )
   
    rcl_amount = add_test_billing - remove_test_billing
    
    amount = amount - remove_test_billing_client_billing
    return{
        'ccdc_name':instance.name,
        'sales_person':instance.sales_manager.user.username if instance.sales_manager else None,
        'centre_type':instance.center_type,
        'total_booking_count':tbc,
        # 'total_revenue':sum([i for i in total_rev if i is not None]),
        'total_revenue':amount,
        'rcl_share':rcl_amount
    }

Here i am calculating total_booking_count, total_revenue and rcl_share. I want to filter these data with start_date and end_date and accordingly these calculation will affect. Suppose if i selected date range 5 days then it will only show calculation of those 5 days. This is last thing i want to do. Is it possible to apply filter in serializer itself? Any help would be appreciated. Thank you !!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source