0

データベースを作成する必要があり、すべてのエントリがデータベースに入力されているか、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" のエントリには上記のエラーが表示されます。 .

この問題を回避するにはどうすればよいですか?

4

1 に答える 1

4

Trialあなたが送ったモデルは正しいものですか?

この ValidationError は、ドキュメントで ReferenceField を宣言したときに、参照先のドキュメントを保存する前にこのドキュメントを保存しようとするとスローされます (Mongoengine は、参照のクラスと ObjectId を含む辞書として MongoDB の参照フィールドを表します)。

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()
于 2014-12-17T12:09:04.690 に答える