Skip to content Skip to sidebar Skip to footer

Save Reference Field Mongoengine

Simplified, I have 2 Document objects: Resource and Cable class Cable(db.Document): _id = db.ObjectIdField() socket = db.EmbeddedDocumentField(Socket) class Resource(db.Do

Solution 1:

Tried with this workaround passing the DBRef of the document and it works.

@resources.route('/<r_id>/add_cabling', methods=['GET'])defset_connector(r_id):
    r = Resource.objects(id=r_id).get()
    c = Cable.objects().first()
    c.save() #revalidate here
    r.cable=c.to_dbref()
    r.save()
    return jsonify(r)

It is necessary to perform the save() operation again on the queried object to get the DBRef otherwise it will throw this error:

OperationError: Only saved documents can have a valid dbref

Post a Comment for "Save Reference Field Mongoengine"