Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>groupby()</code> function to sort out the rows that are returned. Here is a small example of grouping datetimes by date:</p> <pre><code>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)) </code></pre> <p>As you can see, you have to provide <code>groupby()</code> with a <code>key</code> 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 <code>[1]</code> and then calls the Python <code>datetime</code> method <code>date()</code> on it to extract the date part without the hours and minutes. The output of the script looks like this (the <code>pprint()</code> function is just a fancy "print" statement that indents the output, it won't be needed in your Django code!):</p> <pre><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))] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload