如何订购串联的查询集? 我可以进行for循环并打印每个日期
for r in requests:
print(r.game.date)
但是我不能订购查询集。
views.py
class Games(TemplateView):
template_name = ...
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
user = CustomUser.objects.get(pk=self.request.user.pk)
team = Team.objects.get(team=user)
requests_single = Request.objects.filter(content_type=ContentType.objects.get_for_model(CustomUser)).filter(object_id=self.request.user.pk)
requests_team = Request.objects.filter(content_type=ContentType.objects.get_for_model(Team)).filter(object_id=team.pk)
requests = requests_single | requests_team
requests.order_by('-game.date')
context['requests'] = request
return context
model.py
class Forderung(models.Model):
game = models.ForeignKey(Spiel, on_delete=models.CASCADE, null=True)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
答案 0 :(得分:1)
order_by
不会对查询集进行适当排序,而是返回一个新的查询集。试试:
requests = requests.order_by('-game.date')
答案 1 :(得分:1)
您需要使用双下划线语法表示联接。
requests = requests.order_by('-game__date')