我有作为项目列表的 Django 模型,我希望每个列表上的项目具有单独的排序顺序。也许列表 1 将按名称排序行项目,按日期列出 2,并按优先级列出 3。
这些模型或多或少看起来像这样:
class ListItem(models.Model):
priority = models.IntegerField(choices=PRIORITY_CHOICES)
name = models.CharField(max_length=240)
due_date = models.DateTimeField(null=True, blank=True)
list = models.ForeignKey(List)
现在我在我的视图中使用这个查询:
lists = List.objects.filter(user=request.user)
return render_to_response('showlists.html', {'lists': lists })
我读过的例子暗示我可以做这样的事情:
lists = List.objects.filter(user=request.user).select_related().order_by("items.name")
假设这有效,它将为我提供每批项目具有相同排序顺序的列表集合。我将如何对每个列表的相关项目进行不同的排序?