【问题标题】:MongoEngine ValidationErrorMongoEngine ValidationError
【发布时间】:2015-02-15 18:49:14
【问题描述】:

我必须创建一个数据库以及使用 python shell 检查是否所有条目都输入到数据库中。

我写了一个叫 Trial 的类

class Trial(db.Document):
    project_name = db.StringField(max_length=255,required=True)
    list_of_materials = db.ListField(db.EmbeddedDocumentField('List_Of_Materials'))
    abstract = db.StringField(max_length=255,required=True)
    vehicle = db.StringField(max_length=255,required=False)
    responsibilities = db.ListField(db.EmbeddedDocumentField('Responsibilities')) 

我定义了类 List_of_Materials 和 Responsibilities 如下:

class Responsibilities(db.EmbeddedDocument):
    employee = db.StringField(max_length=255, required = True)
    objective = db.StringField(max_length=255, required = True)

class List_Of_Materials(db.EmbeddedDocument):
    mat_name = db.StringField(max_length=255, required=True)
    mat_type = db.StringField()
    mat_comments = db.StringField(max_length = 255)

现在我使用 python shell 进入数据库。

trial_test = Trial(project_name = 'nio field trip management',
                list_of_materials = [List_Of_Materials(mat_name = 'Laptop')],
                abstract = 'All is well that ends well',
                vehicle = Vehicle(veh_name='My Laptop',veh_num='GA07EX1234'),
                responsibilities = [Responsibilities(employee='Prashant',objective='Setup the Website')],

我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 12, in <module>
  File "C:\Anaconda\lib\site-packages\mongoengine\base\document.py", line 85, in __init__
    value = field.to_python(value)
  File "C:\Anaconda\lib\site-packages\mongoengine\base\fields.py", line 261, in to_python
    self.error('You can only reference documents once they'
  File "C:\Anaconda\lib\site-packages\mongoengine\base\fields.py", line 124, in error
raise ValidationError(message, errors=errors, field_name=field_name)
mongoengine.errors.ValidationError: You can only reference documents once they have been saved to the database

代码的第 12 行是responsibilities=db.ListField(db.EmbeddedDocumentField('Responsibilities'))

我可以从上述错误中解释的是,我们必须首先进入类 "Responsibilities" 和 "List_Of_Material" ,但是 "List_Of_Material" 中的条目没有显示任何错误,而 "Responsibilities" 中的条目显示上面的错误。

我可以做些什么来避免这个问题?

【问题讨论】:

    标签: python mongodb flask mongoengine flask-mongoengine


    【解决方案1】:

    您确定您发送的Trial 型号是正确的吗?

    当您在文档中声明 ReferenceField 时会引发此 ValidationError,但您尝试在保存引用的文档之前保存此文档(Mongoengine 将 MongoDB 中的引用字段表示为包含类和引用的 ObjectId 的字典)。

    EmbeddedDocumentField 不是ReferenceField。它们在您保存主文档的同时保存。因此,我认为您的错误不是来自list_of_materialsresponsibilities 属性。如果您在示例中删除车辆分配,则此代码可以完美运行。

    鉴于您的代码示例,我猜有一个类似的类

    class Vehicle(db.Document):
        veh_name = db.StringField()
        veh_num = db.StringField()
    

    你的模型是:

    class Trial(db.Document):
        project_name = db.StringField(max_length=255, required=True)
        list_of_materials = db.ListField(db.EmbeddedDocumentField('List_Of_Materials'))
        abstract = db.StringField(max_length=255, required=True)
        vehicle = db.ReferenceField(Vehicle)
        responsibilities = db.ListField(db.EmbeddedDocumentField('Responsibilities'))
    

    然后,你的例子应该是:

    trial_test = Trial(
         project_name = 'nio field trip management',
         list_of_materials = [List_Of_Materials(mat_name = 'Laptop')],
         abstract = 'All is well that ends well',
         vehicle = Vehicle(veh_name='My Laptop',veh_num='GA07EX1234'),
         responsibilities = [Responsibilities(employee='Prashant',objective='Setup the Website')]
    )
    trial_test.vehicle.save()  # Saving the reference BEFORE saving the trial.
    trial_test.save()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 2022-07-13
      相关资源
      最近更新 更多