0

我必须创建一个数据库以及检查所有条目是否正在使用 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" 中的条目显示上述错误.

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

4

1 回答 1

4

您确定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()
于 2014-12-17T12:09:04.690 回答