title: How to separate umongo's object document models from the main web app or how to avoid calling @instance.register where document models are defined?
body: <p>While creating a web app using Flask or FastAPI, there would be a <em>main.py</em> file that basically instantiates and runs everything. I think that is also the right place for the database connections and initialization. So ideally I'd like to have a separate <em>model.py</em> file that basically just has the object document mapping definitions and nothing else.</p>
  <p>Is it possible to do something like that in umongo?</p>
  <p>I mean we need to call <code>@instance.register</code> above every object document map class. But if that is in a separate file and the DB is not initialized there, then in that file there are no instances. The instance would be declared in the <em>main.py</em> file.</p>
  <p>For example, when you use Tortoise, it allows you to pass the whole <em>model.py</em> file as a module and register it with FastAPI like the following -</p>
  <pre class="lang-py prettyprint-override"><code>register_tortoise(
      app,
      db_url=os.environ.get(&quot;DATABASE_URL&quot;),
      modules={&quot;models&quot;: [&quot;models.model&quot;]}, #model -&gt; model.py which has all the class definitions
      generate_schemas=True
  )
  </code></pre>
  <p>For demo, you can have the following as <code>model.py</code> file contents -</p>
  <pre class="lang-py prettyprint-override"><code>from umongo import Document
  from umongo.fields import StringField, URLField, DateTimeField
  from datetime import datetime
  
  class WebData(Document):
      url = URLField()
      summary = StringField()
      created_at = DateTimeField(missing=datetime.now)
  
      def __str__(self):
          return self.url
  </code></pre>
  <p>And the following main.py file that uses FastAPI -</p>
  <pre class="lang-py prettyprint-override"><code>from fastapi import FastAPI
  from pymongo import MongoClient
  from umongo import Instance
  
  #db = MongoClient().test
  #instance = Instance(db)
  
  app = FastAPI()
  
  @app.get('/ping')
  async def pong():
      return {'ping': 'pong'}
  
  
  if __name__ == &quot;__main__&quot;:
      import uvicorn
      uvicorn.run(&quot;app.main:app&quot;, host=&quot;0.0.0.0&quot;, port=8000, reload=True)
  </code></pre>