python - MongoEngine 模式 - 名称错误

标签 python mongodb mongoengine

我在名为 model.py 的文件中有以下模式

from mongoengine import *

class Subject(Document):
    uri = StringField(required=True)
    resources = ListField(ReferenceField(ResourceSubject))

class ResourceSubject(Document):
    subject = ReferenceField(Subject,reverse_delete_rule=CASCADE)
    resource = ReferenceField(Resource)

class Resource(EmbeddedDocument):
    uri = StringField()
    title = StringField()
    snippet = StringField()
    image = StringField()
    source = StringField()
    adapter = StringField()

出于某种原因,当我尝试初始化一个主题时,subj = Subject(uri="hello").save() 我收到一个名称错误:NameError: name 'ResourceSubject ' 未定义

我不明白原因,我猜这与框架有关?我尝试将类分离到单个文件中,然后导入,但我仍然遇到相同的错误。我错过了什么?

此行引发错误:resources = ListField(ReferenceField(ResourceSubject))

最佳答案

ResourceSubject 放入引号:

resources = ListField(ReferenceField('ResourceSubject'))

关于python - MongoEngine 模式 - 名称错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29322903/

相关文章:

mongodb - 用两个条件检查记录的最简单方法是什么,如果不存在则插入,如果存在一个或多个,则全部更新

python - 如何在python中使用mongoengine在ListField上添加索引?

python - 如果逗号不在括号之间,则用逗号分隔,同时允许字符在括号之外并在同一逗号分隔中

node.js - 无法在 Express 中从 MongoDB 检索数据

node.js - 为什么按时间范围查询mongoDB不起作用?

node.js - Ember、Ember 数据、MongoDB _id 和命名空间

python - 我什么时候会在 monogoengine 的内置 JSON 序列化(from_json 和 to_json)上使用像 marshmallow 这样的外部序列化器?

python - 控制台颜色 (Windows)

python - 访问 Mako 命名空间中定义的变量

python 3 : how to use the press Ctrl+X (cut) and Ctrl+V using pynput?