Django按日期排序(天) [英] Django sorting by date(day)

查看:769
本文介绍了Django按日期排序(天)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想先排序模型,然后按分数排序,这意味着我想在每一天看到最高得分的文章。

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.

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)

此答案 Django Datetime字段查询 - 按时间/小时排序表明,我使用'__ day'与我的 date_modified 如: p>

This answer Django Datetime Field Query - Order by Time/Hour suggests that I use '__day' with my date_modified as in:

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

但是,我发现与帖子相同的错误,所以我甚至不确定它应该工作这样。

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

我发现其他答案 django按日期在datetime / datetime中提取日期,使用 .extra

I found other answers django order by date in datetime / extract date from datetime using .extra:

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

这适用于没有其他条件的过滤,但如果我在过滤器上应用一个条件,例如一个类别:

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

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

我收到此错误:

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

不知道这里发生了什么,帮助将不胜感激。

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

推荐答案

from django.db.models import DateTimeField
from django.db.models.functions import Trunc

Article.objects.order_by(
    Trunc('date_modified', 'date', output_field=DateTimeField()).desc(),
    '-score')




  • Trunc() (Django 1.10)

  • order_by()

    • Trunc() (Django 1.10)
    • order_by()
    • 这篇关于Django按日期排序(天)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆