mongoengine ReferenceField抛出错误

时间:2018-11-06 07:03:03

标签: mongoengine flask-restplus

我用class JsonFormWrapper extends React.Component<PropsT> { render() { return ( <React.Fragment> {this.props.jsonResponse ? ( <Form schema={this.props.jsonResponse.schema} uiSchema={this.props.jsonResponse.uiSchema} /> ) : null} </React.Fragment> ); } } export default JsonFormWrapper; flask-restplus制作了一个简单的Board api系统

下面的代码是我的mongoenginemodel

[模型]

serializer

[序列化器]

class Article(Document):
    no = SequenceField()
    subject = StringField(required=True)
    content = StringField(required=True)
    userid = ReferenceField(User, required=True)
    comments = ListField(EmbeddedDocumentField(Comment))
    created_at = DateTimeField(default=datetime.datetime.now())
    updated_at = DateTimeField(default=datetime.datetime.now())

class User(Document):
    no = SequenceField()
    userid = StringField(unique=True, required=True)
    userpw = StringField(required=True)
    created_at = DateTimeField(default=datetime.datetime.now())

class Comment(EmbeddedDocument):
    content = StringField(required=True)
    userid = ReferenceField(User, required=True)
    created_at = DateTimeField(default=datetime.datetime.now())

每篇文章都必须包含from marshmallow import Schema, fields class UsersSchema(Schema): class Meta: fields = ('no', 'userid', 'userpw', 'created_at') class UserSchema(Schema): class Meta: fields = ('no', 'userid', 'userpw', 'created_at') class ArticleSchema(Schema): class Meta: fields = ('no', 'subject', 'content', 'userid', 'comments', 'created_at', 'updated_at') class ArticleListSchema(Schema): class Meta: fields = ('no', 'subject', 'content', 'userid', 'comments', 'created_at', 'updated_at') 模型所引用的用户ID。

因此我将User定义为userid = ReferenceField(User, required=True)模型。

插入数据可以正常工作。但是当我访问列表数据时,它会引发错误。

Article

(当前,mongoengine.errors.DoesNotExist: Trying to dereference unknown document DBRef('user', 'john')已插入{ "_id" : ObjectId("5be136061b48d90b052da22d"), "no" : 18, "subject" : "dasdsadsa", "content" : "userid", "userid" : "john", "comments" : [ ], "created_at" : ISODate("2018-11-06T15:34:33.856Z"), "updated_at" : ISODate("2018-11-06T15:34:33.856Z") }

插入功能在这里。

article

我不知道为什么会发生错误。

这里有解决方案吗?

谢谢。

0 个答案:

没有答案