gpt4 book ai didi

python - MongoAlchemy 中的数组定义建模

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

我尝试在 Flask 中为我的 MongoAlchemy 类建模。这是我的模型:

{
"_id" : ObjectId("adfafdasfafag24t24t"),
"name" : "sth."
"surname" : "sth."
"address" : [
{
"city" : "Dublin"
"country" : "Ireland"
}
]
}

这是我的 Documents.py 类:

   class Language_School(db.Document):
name = db.StringField()
surname = db.StringField()
address = db.???

如何在 Flask 中像数组模型(地址)一样定义它?

编辑:我已经像提问之前一样尝试过我的模型。但我遇到了像“AttributeErrorAttributeError:“列表”对象没有属性“项目”。

class Address(db.Document):
city = db.StringField()
country = db.StringField()

class Language_School(db.Document):
name = db.StringField()
surname = db.StringField()
address = db.DocumentField(Address)

最佳答案

您询问的是 ListField据我了解。

虽然,我不确定你是否真的需要一个列表来存储地址,为什么不使用特殊的文档类型 - DocumentField ,按照示例 here :

class Address(Document):
street_address = StringField()
city = StringField()
state_province = StringField()
country = StringField()

class User(Document):
name_index = Index().ascending('name').unique()
name = StringField()
email = StringField()

address = DocumentField(Address)

def __str__(self):
return '%s (%s)' % (self.name, self.email)

关于python - MongoAlchemy 中的数组定义建模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38377813/

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