【问题标题】:Mongoengine Error while adding data to the database将数据添加到数据库时出现 Mongoengine 错误
【发布时间】:2018-10-21 06:24:45
【问题描述】:

我刚开始使用 mongoengine,我参考了http://docs.mongoengine.org/tutorial.html 给出的教程,当我尝试将 Post 数据添加到数据库时遇到错误

mongoengine.errors.ValidationError: ValidationError (Post.TextPost:None) (A ReferenceField only accepts DBRef, LazyReference, ObjectId or documents: ['author']

我已添加文件的屏幕截图。

The main app.py file and the directory structure

users.py file

posts.py file

comments.py file

【问题讨论】:

    标签: python python-3.x mongodb mongodb-query mongoengine


    【解决方案1】:

    验证失败的原因是因为您将帖子作者分配为字符串(例如,post1.author = 'Pratik'),而您应该将其分配给 User 实例:

    post1.author = User(firstname='Pratik', lastname='Tester', email='Pratik.Tester@test.it')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 2018-10-02
      • 1970-01-01
      • 2018-02-26
      相关资源
      最近更新 更多