Django:如何订购由OneToOneField的日期设置的查询?

时间:2019-01-22 05:58:04

标签: python django django-models django-queryset

我有两节课:

class Foo(models.Model):
    date = models.DateTimeField(auto_now_add = True)
    ...

class Bar(models.Model):
    foo = models.OneToOneField(
        Foo,
        on_delete = models.CASCADE,
        primary_key = True
    )
    ...

如何订购Bar's对象,如下所示:

query_set = Bar.objects.all().order_by('-foo.date')

1 个答案:

答案 0 :(得分:2)

您可以通过__访问外键值 像这样

query_set = Bar.objects.all().order_by('-foo__date')