'Dynamically adding entities to PonyORM database from class definition
Having class
from pony.orm import Required
class Place:
name = Required(str)
country = Required(str)
Is it possible to generate PonyORM entity from this definition with
from pony.orm import database
def initialize_database():
db=database()
adds_db_entity_connection(db, Place)
db.bind(provider="sqlite",filename=":memory:")
db.generate_mapping(check_tables=True, create_tables=True)
return db
I have tried creating these from dictionary like {"name": Required(str), "country":Required(str)} with
def bind_class(name, db, prop_dict):
return type(
name,
(
db.Entity,
),
prop_dict,
)
But this leads to
TypeError: Entity classes cannot contain __slots__ variable
I am aware of dynamic factory solution but this does not really apply to here. I have also tried adding db.Entity to Place.__bases__ but this did not work either as I suspect the act of inheritance is required to formulate database relation to the entities. Is the approach completely impossible?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
