How can I Filter Two Models at Once using Date Range in Django with Forms and Count

Viewed 11

I am working on a Django project and I want to query two different Models; Income and Expenditure using Date Range with Django Forms. I can query one Model at once but don't know how I can query both the Income and Expenditure table at the same time and as well count number of objects in both Models.

Below are my Models:

class Expenditure(models.Model):  
    description = models.CharField(max_length=100, null=False)
    source = models.CharField(max_length=100, choices=CATEGORY_EXPENSES, null=True)
    staff = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    amount = models.PositiveIntegerField(null=False)
    Remarks = models.CharField(max_length=120, null=True)
    date = models.DateField(auto_now_add=False, auto_now=False, null=False)
    addedDate = models.DateTimeField(auto_now_add=True)



    class Meta:
        verbose_name_plural = 'Expenses'

    def __str__(self):
        return self.description

class Income(models.Model): 
    description = models.CharField(max_length=100, null=False)
    staff = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    amount = models.PositiveIntegerField(null=False)
    remarks = models.CharField(max_length=120, null=True)
    date = models.DateField(auto_now_add=False, auto_now=False, null=False)
    addedDate = models.DateTimeField(auto_now_add=True)

class Meta: 
    verbose_name_plural = 'Income Sources'

def __str__(self):
    return self.description

My Django forms as in forms.py:

class IncomeSearchForm(forms.ModelForm):
    start_date = forms.DateField(required=True, widget = IncomeDateField)
    end_date = forms.DateField(required=True, widget = IncomeDateField)
    class Meta:
        model = Income
        fields = ['start_date', 'end_date']

My views.py where I am able to use Date Range with Search form to query Income Model for Date Range Report.

def SearchIncomeRange(request):
 #check for user session
if 'user' not in request.session:
    return redirect('cashier-login')
else:
    context = {}
    searchForm = IncomeSearchForm(request.POST or None)
    if searchForm:
        listIncome = Income.objects.filter(date__range=[searchForm['start_date'].value(),searchForm['end_date'].value()])

    else:
        listIncome = Income.objects.all()

    paginator = Paginator(listIncome, 5)
    page = request.GET.get('page')
    paged_listIncome = paginator.get_page(page)
    
    #Calculate total amount of Date Range Result
    total = listIncome.aggregate(total = 
    Sum('amount')).get('total') or 0

    context.update({
    'listIncome':paged_listIncome,
    'searchForm':searchForm,
    'total':total,
    })

    return render(request, 'cashier/search_income_range.html',context)

Someone should please help with the best way of filtering through my Income and Expenditure Models with their respective Totals and possibly count objects in both Models. Thanks.

0 Answers
Related