Base Pony Model in PonyORM

56 Views Asked by At

I have a project with many Pony Models where all of them share some fields:

uuid = PrimaryKey(str)
deleted = Optional(bool, sql_default=False)
created_at = Required(datetime, sql_default=get_now(settings.DB_PROVIDER))
updated_at = Required(datetime, sql_default=get_now(settings.DB_PROVIDER))

I want to create a BasePonyModel like:

class BasePonyModel:
    uuid = PrimaryKey(str)
    deleted = Optional(bool, sql_default=False)
    created_at = Required(datetime, sql_default=get_now(settings.DB_PROVIDER))
    updated_at = Required(datetime, sql_default=get_now(settings.DB_PROVIDER))

I would then use that Base model like:

class SomePonyModel(BasePonyModel(table='table_name', database=db)):
   name = Optional(str)
   description = Optional(str)
   ...

The problem is that I cannot manage to get it working in a generic way so I could use the same base model for multiple models. I tried overriding init and new, but I had no success doing it. Since I have multiple databases I have to pass in the db: Database object to make it work. If you have any clue how this can be done (if it can be done) please point me in the right direction.

I already posted an issue on their repo for that since I think this "BasePonyModel" logic would help a lot of people to clean up their code.

0

There are 0 best solutions below