我必须创建一个数据库,以及如何检查是否所有条目都被输入到数据库中,或者没有使用python。
我写了一个叫做审判的课
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和职责定义为:
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向数据库输入一个条目。
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”中的条目显示了上面的错误。
我怎样才能避免这个问题呢?
发布于 2014-12-17 12:09:04
您确定您发送的Trial模型是正确的吗?
当在文档中声明ReferenceField时,会引发此ReferenceField,但在保存引用文档之前尝试保存此文档(Mongoengine将MongoDB中的引用字段表示为包含引用的类和ObjectId的字典)。
EmbeddedDocumentField不是ReferenceField。它们在保存主文档的同时保存。因此,我不认为您的错误来自list_of_materials或responsibilities属性。如果在示例中删除车辆分配,则此代码运行良好。
考虑到您的代码示例,我猜有一个类类似
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()https://stackoverflow.com/questions/27521911
复制相似问题