【发布时间】:2017-02-22 12:17:06
【问题描述】:
我已经搜索了很长时间,并且知道关于 sof 的几个答案,但即使我的问题很简单,也没有一个解决方案对我有用:
我需要什么(使用 postgres + django 1.10):我在日期时间字段中有很多行,其中包含许多重复日期(=天)。我想要一个每个日期/天包含一行/对象的查询集。
fk | col1 | colX | created (type: datetime)
----------------------------------------------
1 | info | info | 2016-09-03 08:25:52.142617+00:00 <- 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 <- 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 <- 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.
什么是最好的(性能 + pythionic/django)方法来做到这一点。我的模型/表格将有很多行(>百万)。
编辑 1
结果必须先用 fk 过滤(例如 WHERE fk = 1)。
我已经尝试过最明显的事情,例如
MyModel.objects.filter(fk=1).order_by('created__date').distinct('created__date')
但出现以下错误:
django.core.exceptions.FieldError:无法将关键字“日期”解析为字段。不允许加入“已创建”。
...all() 和通过 Meta 类而不是查询方法 order_by() 进行相应排序的相同错误...
在这种特定情况下,是否有人可能更了解此错误?
【问题讨论】:
标签: python django postgresql datetime