如何在peewee元类中使用默认排序

时间:2015-04-19 09:19:25

标签: python peewee

我使用的是Peewee ORM。

我有一个这样的课程:

class Sample(PMBaseModel):
    id = peewee.PrimaryKeyField()
    location = peewee.CharField(max_length=255)
    position = peewee.IntegerField()

    class Meta:
        db_table = 'sample'

如何使用Meta Class设置默认排序并定义订单类型?

2 个答案:

答案 0 :(得分:2)

我找到了答案,感谢@giaosudau。

class Sample(PMBaseModel):
    id = peewee.PrimaryKeyField()
    location = peewee.CharField(max_length=255)
    position = peewee.IntegerField()

    class Meta:
        db_table = 'sample'
        order_by = ['-location']

如果使用' - '在order_by部分的第一个字段名称中,peewee执行DESC对提交的排序。

答案 1 :(得分:1)

按照此说明https://peewee.readthedocs.org/en/latest/peewee/models.html#model-options

我们使用order_by字段列表用于默认排序

然后

class Sample(PMBaseModel):
    id = peewee.PrimaryKeyField()
    location = peewee.CharField(max_length=255)
    position = peewee.IntegerField()

    class Meta:
        db_table = 'sample'
        order_by = ['location']