Django - distinct rows/objects distinguished by date/day from datetime field
<p>I'v searched quite a while now and know about several answers on sof but none of the solutions does work at my end even if my problem is pretty simple:</p>

<p>What I need (using postgres + django 1.10): I have many rows with many duplicate dates (=days) within a datetime field. I want a queryset containing one row/object each date/day.</p>

<pre><code>fk | col1 | colX | created (type: datetime)
----------------------------------------------
1  | info | info | 2016-09-03 08:25:52.142617+00:00 &lt;- get it (time does not matter)
1  | info | info | 2016-09-03 16:26:52.142617+00:00
2  | info | info | 2016-09-03 11:25:52.142617+00:00
1  | info | info | 2016-09-14 16:26:52.142617+00:00 &lt;- get it (time does not matter)
3  | info | info | 2016-09-14 11:25:52.142617+00:00
1  | info | info | 2016-09-25 23:25:52.142617+00:00 &lt;- get it (time does not matter)
1  | info | info | 2016-09-25 16:26:52.142617+00:00
1  | info | info | 2016-09-25 11:25:52.142617+00:00
2  | info | info | 2016-09-25 14:27:52.142617+00:00
2  | info | info | 2016-09-25 16:26:52.142617+00:00
3  | info | info | 2016-09-25 11:25:52.142617+00:00
etc.
</code></pre>

<p>Whats the best (performance + pythionic/django) way to do this. My model/table is going to have many rows (>million).</p>

<p><strong>EDIT 1</strong></p>

<p>The results must be filtered by a fk (e.g. WHERE fk = 1) first.</p>

<p>I already tried the most obvious things such as</p>

<pre><code>MyModel.objects.filter(fk=1).order_by('created__date').diââstinct('created__datââe') 
</code></pre>

<p>but got following error:</p>

<blockquote>
  <p>django.core.exceptions.FieldError: Cannot resolve keyword 'date' into field. Join on 'created' not permitted.</p>
</blockquote>

<p>...same error with all() and respective ordering through class Meta instead of query-method order_by()...</p>

<p>Does somebody maybe know more about this error in this specific case?</p>

<p>you can use a Queryset to get the results from your table by a distinct on the created value because you are using postgresql.</p>

<p>Maybe a query like this should do the work :</p>

<pre><code>MyModel.objects.all().distinct('created__date')
</code></pre>

<p>I refer you too the queryset documentation of django : <a href="https://docs.djangoproject.com/fr/1.10/ref/models/querysets/#distinct" rel="nofollow">https://docs.djangoproject.com/fr/1.10/ref/models/querysets/#distinct</a></p>

python django postgresql datetime