如何获得peewee结果长度

时间:2015-07-29 08:02:25

标签: python orm peewee

我在python中使用peewee orm。

我有一个查询:

userOrganizations = (UserOrganization
    .select(UserOrganization,Organization)
    .join(Organization)
    .where(UserOrganization.user==user.user_id)
    .aggregate_rows()
)

我想得到userOrganizations变量的长度。有没有像userOrganizations.length()这样的方法?

1 个答案:

答案 0 :(得分:3)

根据peewee documentation,您可以使用count()功能,即:

userOrganizations.count()

如果您担心可能会运行额外的数据库查询,您可以将结果转换为列表并获取长度,例如:

len(list(userOrganizations))

第二种技术的来源:this question

相关问题