Declaring classes that map the db in ponyORM inherits from database
as a global variable.
from pony.orm import *
db = Database()
class MyEntity(db.Entity):
attr1 = Required(str)
Is there a way to declare Entities without relying on a global variable?
Maybe something in the line of
class MyEntity(get_db().Entity):
but with control over when the classes are declared so that get_db() returns the appropriate db.
--
We have a few situations in which this would be desirable.
- Mixing databases
Using multiple databases with Pony ORM
This is the solution we have currently running. But having inner Classes is ugly, specially because there are a lot of them.
Maybe we could make them go to the global space with global? or import the models within this inner function?
- unit testing
Having a global variable causes errors in unit testing or makes it overly complicated because each setUp must check if the database is already bound, and ensure it is the right database.
- mapping views
We would like to map views as pony classes. Declare some ORM classes, generate the tables, create the views, declare the ORM view classes.
--
We took a look to other projects that also rely on global objects like flask or celery but we didn't find a solution that would apply to ponyORM.
Maybe it's just not possible and the database object has to be defined and that's it.