how to filter by month from template

10 views
Skip to first unread message

Gautam Ankul

unread,
Feb 23, 2022, 2:39:14 AM2/23/22
to Django users
HERE STACKOVER FLOW LINK OF QUESTIONS

---------------------------------------------------------------------------------------------------------------------------------------------

I'm attempting to channel a datetime field by month and year. Though no one can really say why while entering both month and year I get back a vacant set returned.

Model

class SpareParts(models.Model): vehicle = models.ForeignKey(Vehicle, on_delete=models.CASCADE) amount = models.IntegerField(blank=False, null=False) date = models.DateField(blank=False, null=False)

And i want to filter on the basis of vehicle and month vise and here is mine view VIEWS.py

def view_spare(request): sparepart = SpareParts.objects.all() vehicle_filter = request.POST.get('vehicle') # get the value of the date field month_filter = request.POST.get('month') # get the value of the date field if vehicle_filter: if month_filter: sparepart = sparepart.filter(vehicle=vehicle_filter,date__month=month_filter).aggregate(Sum('amount')) return render(request,'invoice/spare_parts_list.html',{'sparepart':sparepart})

and i want render the whole month sum of amount in template

Reply all
Reply to author
Forward
0 new messages