如何在peewee中创建自引用多对多字段?

时间:2017-12-19 04:46:34

标签: python postgresql peewee

我第一次使用peewee Python ORM(带Playhouse模块的Postgresql),我想做以下事情: class Person(BaseModel): followers = ManyToManyField(rel_model=Person, related_name='following') 但是我得到一个NameError,因为当我尝试将它用作参数时没有定义Person。有没有一种干净的方法来使用ManyToManyField做我想要的,或者我只需要创建一个单独的联结表,好像ManyToManyField功能不存在一样?

1 个答案:

答案 0 :(得分:2)

你想要做这样的事情:

class Person(Model):
    name = TextField()

class Follower(Model):
    from_person = ForeignKeyField(Person, related_name='followers')
    to_person = ForeignKeyField(Person, related_name='followed_by')