Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4038664
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:30:03+00:00 2026-05-20T12:30:03+00:00

MyModel.objects.filter(created_at__gte=’2011-03-09′, created_at__lte=’2011-03-11′).values(‘created_at’,’status’).annotate(status_count=Count(‘status’)) The above query has the problem with the created_at datetime field. Is

  • 0
MyModel.objects.filter(created_at__gte='2011-03-09', created_at__lte='2011-03-11').values('created_at','status').annotate(status_count=Count('status'))

The above query has the problem with the created_at datetime field. Is it possible to tune the above query to ignore the time value and use the date value alone while performing group by?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:30:03+00:00Added an answer on May 20, 2026 at 12:30 pm

    I am not sure whether Django’s ORM can perform a conversion of datetimes to dates in the middle of a query. You could, though, do the query first, get the results, then use the Python groupby() function to sort out the rows that are returned. Here is a small example of grouping datetimes by date:

    from pprint import pprint
    from datetime import datetime
    from itertools import groupby
    
    rows = [('Frodo party', datetime(3018, 9, 22, 10, 38)),
            ('Nazgul defeat Rangers', datetime(3018, 9, 22, 11, 57)),
            ('Frodo finishes packing', datetime(3018, 9, 23, 10, 59)),
            ('Gandalf tames Shadowfax', datetime(3018, 9, 23, 13, 11)),
            ('Gandalf crosses the Isen', datetime(3018, 9, 24, 18, 46))]
    
    for key, values in groupby(rows, key=lambda row: row[1].date()):
        print('-')
        pprint(key)
        pprint(list(values))
    

    As you can see, you have to provide groupby() with a key function that takes one of the objects that it is grouping and extracts the value by which the grouping should take place — in this case, it grabs the second item in each row with [1] and then calls the Python datetime method date() on it to extract the date part without the hours and minutes. The output of the script looks like this (the pprint() function is just a fancy “print” statement that indents the output, it won’t be needed in your Django code!):

    -
    datetime.date(3018, 9, 22)
    [('Frodo party', datetime.datetime(3018, 9, 22, 10, 38)),
     ('Nazgul defeat Rangers', datetime.datetime(3018, 9, 22, 11, 57))]
    -
    datetime.date(3018, 9, 23)
    [('Frodo finishes packing', datetime.datetime(3018, 9, 23, 10, 59)),
     ('Gandalf tames Shadowfax', datetime.datetime(3018, 9, 23, 13, 11))]
    -
    datetime.date(3018, 9, 24)
    [('Gandalf crosses the Isen', datetime.datetime(3018, 9, 24, 18, 46))]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a difference in the result between: MyModel.objects.filter(pk=1) and MyModel.objects.get(pk=1) If there is
Snippet taken from this question from django.db.models import F ... MyModel.objects.filter(id=...).update(hit_count=F(hit_count)+1) It was suggested
MyModel.objects.filter(field__icontains=value) returns all the rows whose field contains value . How to do the
In Eclipse RCP way of doing things, where should I keep my model objects?
my model is basically a chain of objects linked by a foreign key: class
Is it possible to filter a Django queryset by model property? i have a
I'm trying to fetch all expired objects for a model in my Django application.
I am using the Django query filter __search to perform a full text search
I have a model with a custom method. Here's the example: class MyModel(models) id
I am using the filter icontains to search for words but I only want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.