データベースを作成する必要があり、すべてのエントリがデータベースに入力されているか、Python シェルを使用していないかを確認する必要があります。
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 シェルを使用してデータベースにエントリを作成しています。
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
コードのLINE 12はresponsibilities=db.ListField(db.EmbeddedDocumentField('Responsibilities'))
上記のエラーから私が解釈できるのは、クラス "Responsibilities" および "List_Of_Material" に最初にエントリする必要があるということですが、"List_Of_Material" のエントリにはエラーが表示されませんが、"Responsibilities" のエントリには上記のエラーが表示されます。 .
この問題を回避するにはどうすればよいですか?