在 Peewee 中加入查询
Join Queries in Peewee
你好 Whosebug 社区,
我刚开始开发 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()
我想显示一个 view/table,它将包含所有 table 的组合信息。我尝试对用户和设备这样做(目前):
query = (Equipment.select(Equipment, User).join(User, JOIN.LEFT_OUTER))
这里我先获取设备值,再获取用户。我尝试使用设备更改用户,反之亦然,但效果不佳。
基本上,我想按以下顺序将所有这三个 table 显示为一个单数 table:
User Name || User Email || Dept_ID || Dept_NAME || EQUIP_ID || Is_Fullset
如果我需要更改模型或查询,请告诉我。任何帮助将不胜感激。
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)
你好 Whosebug 社区,
我刚开始开发 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()
我想显示一个 view/table,它将包含所有 table 的组合信息。我尝试对用户和设备这样做(目前):
query = (Equipment.select(Equipment, User).join(User, JOIN.LEFT_OUTER))
这里我先获取设备值,再获取用户。我尝试使用设备更改用户,反之亦然,但效果不佳。 基本上,我想按以下顺序将所有这三个 table 显示为一个单数 table:
User Name || User Email || Dept_ID || Dept_NAME || EQUIP_ID || Is_Fullset
如果我需要更改模型或查询,请告诉我。任何帮助将不胜感激。
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)