加入Peewee的查询

时间:2017-11-12 23:01:11

标签: python peewee flask-peewee

Hello Stackoverflow社区,

我刚刚开始研究peewee应用程序,很少遇到加入查询。目前,我有以下模型设置:

class Equipment(BaseModel):
    equip_id = CharField(primary_key=True)
    is_fullset = BooleanField(default=False)


class Department(BaseModel):
    dept_id = IntegerField(primary_key=True)
    dept_name = CharField()


class User(BaseModel):
    email = CharField(primary_key=True)
    equip_id_fk = ForeignKeyField(Equipment, related_name="equipments", null=True)
    dept_id_fk = ForeignKeyField(Department, related_name="departments")
    user_name = CharField()

我想显示一个视图/表,它将保存所有表的组合信息。我尝试为用户和设备这样做(现在):

  query = (Equipment.select(Equipment, User).join(User, JOIN.LEFT_OUTER))

这里我首先获得设备值,然后是用户。我尝试用设备更换用户,反之亦然,但没有成功。 基本上,我想按顺序将所有这三个表显示在一个单个表中:

User Name || User Email || Dept_ID || Dept_NAME || EQUIP_ID || Is_Fullset

如果我需要更改模型或查询,请告诉我。任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:1)

query = (User
         .select(User, Equipment, Department)
         .join(Equipment, JOIN.INNER)
         .switch(User)
         .join(Department, JOIN.INNER))
for row in query:
    print(row.user_name, row.email, row.dept_id_fk.dept_id,
          row.dept_id_fk.dept_name, row.equip_id_fk.equip_id,
          row.equip_id_fk.is_fullset)