首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoEngine ValidationError

MongoEngine ValidationError
EN

Stack Overflow用户
提问于 2014-12-17 09:14:05
回答 1查看 8.4K关注 0票数 0

我必须创建一个数据库,以及如何检查是否所有条目都被输入到数据库中,或者没有使用python。

我写了一个叫做审判的课

代码语言:javascript
复制
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和职责定义为:

代码语言:javascript
复制
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向数据库输入一个条目。

代码语言:javascript
复制
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')],

我得到了以下错误:

代码语言:javascript
复制
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”中的条目显示了上面的错误。

我怎样才能避免这个问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-17 12:09:04

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

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

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

考虑到您的代码示例,我猜有一个类类似

代码语言:javascript
复制
class Vehicle(db.Document):
    veh_name = db.StringField()
    veh_num = db.StringField()

你的模型是:

代码语言:javascript
复制
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'))

然后,你的例子应该是:

代码语言:javascript
复制
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()
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27521911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档