두 모델이 있습니다 : 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 보이는거야? 당신이 전체 결과를 정렬하려면
posts_type1 = PostType1.objects.all().order_by('-created_date')
posts_type2 = PostType2.objects.all().order_by('-publish_date')
, 당신은 대신 chain
의 사용자 정의 반복자를 사용할 수 있습니다
나는 똑같은 일을하려했지만 정렬 된 논점은 잘못 말하지 않는 것이 조금 어색했다. 고맙습니다. – geros