Django queryset - 按字段值排序记录 - 首先显示field_name = value的记录

时间:2012-08-08 21:21:17

标签: python django django-queryset

以下是模型:

TYPE_CHOICES = (
('x','Some X'),
('y','Some Y'),
('z', 'Some Z'),
)

class MyModel(models.Model):
    some_type = models.CharField(max_length=1, choices=TYPE_CHOICES)
    desc = models.TextField()
    date_added = models.DateFimeField()

我想首先获得some_type='x'的记录,然后是其他记录(在每个“组”中按date_added排序)

1 个答案:

答案 0 :(得分:0)

使用order_by

MyModel.objects.all().order_by("some_type", "date_added")