Django:根据两个字段之间的差异对模型进行排序
参加这样一个简单的课程。
class MyModel(models.Model):
last_updated = models.DateTimeField(auto_now_add=True)
last_viewed = models.DateTimeField(auto_now_add=True)
我想要做的就是按这两个字段之间的时间对该模型的查询输出进行排序,以便自更新以来未查看的内容显示得比已查看的内容更高。
我觉得我应该能够对表进行注释并根据该注释进行排序,但在注释(或原始 SQL)方面我并不是天才。有人能把我踢向正确的方向吗?
Take a simple class like this.
class MyModel(models.Model):
last_updated = models.DateTimeField(auto_now_add=True)
last_viewed = models.DateTimeField(auto_now_add=True)
All I want to do is sort the output of a query on this model by the time between those two fields so that things that haven't been viewed since they were updated show higher up than things that have.
I feel like I should be able to annotate the table and sort on that annotation, but I'm no genius when it comes to annotations (or raw SQL, for that matter). Could somebody kick me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行以下操作:
请参阅文档以获取详细说明。
You can do something like this:
See the docs for a detailed explanation.