1

您好 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

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

4

1 回答 1

2
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)
于 2017-11-14T17:14:20.347 回答