我有两个模型:PostType1,PostType1.
class PostType1(models.Model):
...
created_date = models.DateTimeField(_('created date'), blank=True, null=True)
class PostType2(models.Model):
...
publish_date = models.DateTimeField(_('publish date'), blank=True, null=True)
我对以下两个进行了查询:
posts_type1 = PostType1.objects.all()
posts_type2 = PostType2.objects.all()
我知道如何链接它们:
posts = chain(posts_type1,posts_type2)
我正在寻找一种按日期降序排序的方法.
这可能吗 ?或者我应该看看原始的sql?
解决方法:
因此,如果您的计划是对两个查询集的并集进行排序,则必须使用sorted方法.我会去做类似的事情:
sorted(chain(posts_type1, posts_type2),
key=lambda x: x.created_date if isinstance(x, PostType1)
else x.publish_date)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。