皮威。我怎样才能 select 所有带有预取字段的连接外键字段的模型项?

Peewee. How can I select all model items with joined foreignkey field with prefetched field?

我有3个模型。

from peewee import (
    PostgresqlDatabase,
    Model,
    CharField,
    ForeignKeyField,
    prefetch,
    JOIN_LEFT_OUTER
)

db = PostgresqlDatabase("***", user='***', password='***')


class Model1(Model):
    name = CharField()

    class Meta:
        database = db


class Model2(Model):
    name = CharField()
    model1 = ForeignKeyField(Model1)

    class Meta:
        database = db


class Model3(Model):
    name = CharField()
    model1 = ForeignKeyField(Model1)

    class Meta:
        database = db


def main():
    model1_prefetched = prefetch(Model1.select(Model1), Model3.select(Model3)).select()
    query = Model2.select(Model2.name, model1_prefetched.c.model1, model1_prefetched.c.model3).join(model1_prefetched, join_type=JOIN_LEFT_OUTER, on=(Model2.model1_id==model1_prefetched.c.id))
    model2_objects = list(query.execute())


if __name__ == '__main__':
    main()

我想 select 来自 Model2 的所有数据与 Model1 合并并预取所有 Model3- objects 到每个 Model1-object。 我不明白我该怎么做。

我做错了什么?

谢谢。

query = Model2.select()
prefetch(query, Model1, Model3)
model3_list = list(query.execute())