1

We are planning to develop an app using Flask and Mongoengine. However, we already have some mongoengine document definitions used in a non flask app, and if possible we would like to refactor those definitions into one package, in order to use it from bothe the flask and the non flask application.

Is this possible? I've read the init code for Flask Mongoengine, and it seems it just overrides some base classes for documents, and pulls the config from the flask app. Also, ive found this (unanswered) question:

Pulling basic mongoengine document definitions into flask-mongoengine

Wenfang Du
  • 8,804
  • 9
  • 59
  • 90

1 Answers1

5

You don't even need Flask-Mongoengine. I'm currently working on Flask app with Mongoengine but I don't even use Flask-Mongoengine. I feel more comfortable using vanilla mongoengine. Just make sure you connect to your existing MongoDB somewhere.

mongoengine.connect(MONGODB_DB, host=MONGODB_HOST, port=MONGODB_PORT)

Joseph Vargas
  • 772
  • 5
  • 17
  • is this all that's required to use mongoengine with a flask app? I mean, i know the extension adds some methods for the queryset and such, but doesnt it add connection handling and so on? thanks – Rama Rivera Apr 02 '18 at 19:32
  • I am not sure if it add connection handling, but it only 2 lines of code to connect and disconnect. [check out this link](https://stackoverflow.com/questions/43866098/mongoengine-close-connection) – Joseph Vargas Apr 03 '18 at 18:48
  • yeah I know it's not big deal connecting and disconnecting, I meant if it was done before and after each request. Nevertheless, we are going with your suggested approach, so thanks! – Rama Rivera Apr 05 '18 at 13:41
  • After looking at the github source, I don't think so. – Joseph Vargas Apr 06 '18 at 15:21