How can I filter a date of a DateTimeField in Django?

Such lookups are implemented in django.views.generic.date_based as follows: {‘date_time_field__range’: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max))} Because it is quite verbose there are plans to improve the syntax using __date operator. Check “#9596 Comparing a DateTimeField to a date is too hard” for more details.

Django Aggregation: Summation of Multiplication of two fields

With Django 1.8 and above you can now pass an expression to your aggregate: from django.db.models import F Task.objects.aggregate(total=Sum(F(‘progress’) * F(‘estimated_days’)))[‘total’] Constants are also available, and everything is combinable: from django.db.models import Value Task.objects.aggregate(total=Sum(‘progress’) / Value(10))[‘total’]

How do I filter query objects by date range in Django?

Use Sample.objects.filter(date__range=[“2011-01-01”, “2011-01-31″]) Or if you are just trying to filter month wise: Sample.objects.filter(date__year=”2011″, date__month=”01”) Edit As Bernhard Vallant said, if you want a queryset which excludes the specified range ends you should consider his solution, which utilizes gt/lt (greater-than/less-than).

How can I combine two or more querysets in a Django view?

Concatenating the querysets into a list is the simplest approach. If the database will be hit for all querysets anyway (e.g. because the result needs to be sorted), this won’t add further cost. from itertools import chain result_list = list(chain(page_list, article_list, post_list)) Using itertools.chain is faster than looping each list and appending elements one by … Read more