Open
Description
files are getting stored in default db even with using switch_db()
Please refer the below code that demostrates the case.
Mongo engine VERSION = 0.10.6
#models.py
from mongoengine import FileField, Document, StringField
from mongoengine import connect, register_connection
connect('ets', alias='default')
register_connection('ets_archive','ets_archive')
class Mediafile(Document):
content_type = StringField(max_length=255, required=True)
file_name = StringField(max_length=1024, required=True)
file_member = FileField( default=None)
from models import Mediafile
from mongoengine.context_managers import switch_db
def save_db():
with switch_db(Mediafile, 'ets_archive') as Mediafile_export_db:
mediafile = Mediafile_export_db()
mediafile.content_type = 'text/plain'
mediafile.file_name = 'new_db.txt'
mediafile.file_member.new_file()
mediafile.file_member.write('new db'.encode("UTF-8"))
mediafile.file_member.close()
mediafile.save()
if __name__ == '__main__':
save_db()
When I run test.py the file is stored to the default database.
Note there Mediafile Document is still stored in the new archive database. Its only the (gridfs)file thats stored in the default database instead of database named 'ets_archive'