Django sorting by date(day)
<p>I want to sort models by day first and then by score, meaning I'd like to see the the highest scoring Articles in each day. </p>

<pre><code>class Article(models.Model):

date_modified = models.DateTimeField(blank=True, null=True)
score = models.DecimalField(max_digits=5, decimal_places=3, blank=True, null=True)
</code></pre>

<p>This answer <a href="https://stackoverflow.com/questions/23599642/django-datetime-field-query-order-by-time-hour">Django Datetime Field Query - Order by Time/Hour</a> suggests that I use <code>'__day'</code> with my <code>date_modified</code> as in:</p>

<pre><code>Article.objects.filter().order_by('-date_modified__day', '-score')
FieldError: Cannot resolve keyword 'day' into field. Join on 'date_modified' not permitted.
</code></pre>

<p>However I get the same error as in the post, so I'm not even sure it should work this way.</p>

<p>I found other answers <a href="https://stackoverflow.com/questions/3652975/django-order-by-date-in-datetime-extract-date-from-datetime">django order by date in datetime / extract date from datetime</a> using <code>.extra</code>: </p>

<pre><code>Article.objects.filter().extra(select={"day_mod": "strftime('%d', date_modified)"}, order_by=['-day_mod', '-score'])
</code></pre>

<p>This works for filtering with no other conditions, but if I apply a condition on the filter such as a category:</p>

<pre><code>Article.objects.filter(category = 'Art').extra(select={'day_mod': "strftime('%d', date_modified)"}, order_by=['-day_mod', '-score'])
</code></pre>

<p>I get this error:</p>

<pre><code>File "/home/mykolas/anaconda2/lib/python2.7/site-packages/IPython/core/formatters.py", line 699, in __call__
printer.pretty(obj)

File "/home/mykolas/anaconda2/lib/python2.7/site-packages/IPython/lib/pretty.py", line 383, in pretty
return _default_pprint(obj, self, cycle)

File "/home/mykolas/anaconda2/lib/python2.7/site-packages/IPython/lib/pretty.py", line 503, in _default_pprint
_repr_pprint(obj, p, cycle)

File "/home/mykolas/anaconda2/lib/python2.7/site-packages/IPython/lib/pretty.py", line 694, in _repr_pprint
output = repr(obj)

File "/home/mykolas/lenv/lib/python2.7/site-packages/django/db/models/query.py", line 234, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])

File "/home/mykolas/lenv/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
self._fetch_all()

File "/home/mykolas/lenv/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())

File "/home/mykolas/lenv/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
results = compiler.execute_sql()

File "/home/mykolas/lenv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
cursor.execute(sql, params)

File "/home/mykolas/lenv/lib/python2.7/site-packages/django/db/backends/utils.py", line 83, in execute
sql = self.db.ops.last_executed_query(self.cursor, sql, params)

File "/home/mykolas/lenv/lib/python2.7/site-packages/django/db/backends/sqlite3/operations.py", line 146, in last_executed_query
return sql % params

TypeError: %d format: a number is required, not unicode
</code></pre>

<p>Don't really know what's going on here, help would be appreciated.</p>