在peewee中加入3桌

时间:2019-05-11 15:58:15

标签: python peewee

我希望能够在peewee中加入多个表。这种情况对我来说很难弄清楚如何使其与peewee一起使用。 我有以下表格: 用户,产品,图片

这是我的模特

class user(BaseModel):
    id = AutoField(primary_key=True)
    username = CharField()
    password = CharField()

class product(BaseModel):
    id = AutoField(primary_key=True)
    id_user = ForeignKeyField(user)
    product_name = CharField()
    product_price = IntegerField()
    product_description = TextField()

class images(BaseModel):
    id = AutoField(primary_key=True)
    id_product = ForeignKeyField(barang)
    image_name = TextField()

我想要类似product.id,user.username,product.product_name,product.price,product.product_description,images.image_name的输出

1 个答案:

答案 0 :(得分:0)

query = (Product
         .select(Product, User, Image)
         .join_from(Product, User)
         .join_from(Product, Image))
for product in query:
    print(product.product_name)
    print(product.id_user.username)
    print(product.images.image_name)

在此处非常详尽地记录了所有内容:http://docs.peewee-orm.com/en/latest/peewee/relationships.html

请阅读文档。