gpt4 book ai didi

python - 模型未定义

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:00 26 4
gpt4 key购买 nike

我使用 Flask 和 Peewee 为 UsersAppointments 创建模型,并且每个模型都有一个相互引用的 ForeignKeyField。问题是,如果我在另一个之上定义一个,Flask 会给我 x is not Define

例如:

class User(Model):
appointments = ForeignKeyField(Appointment, related_name='appointments')

class Appointment(Model):
with_doctor = ForeignKeyField(User, related_name='doctor')

这将返回“User”未定义。我该如何解决这个问题?

最佳答案

您通常不会在关系的双方都定义外键,特别是当关系不是一对一时。

您还应该将 lated_name 设置为对相关模型有意义的内容。对于访问用户约会来说,user.appointments可能是比user.doctor更好的名称。

class User(Model):
pass

class Appointment(Model):
with_doctor = ForeignKeyField(User, related_name='appointments')

关于python - 模型未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38069283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com