How to use mixins to share attributes with pony orm

Viewed 236

Is there any way to share the attribute definition for entities in pony.orm? I have a couple of classes that are not related to each other but I want them to have the same attributes. Currently I copy paste them from class to class but once I change the wording or add/remove an attribute this is lots of manual work.

Is there any way to transform this

class ClassA(db.Entity):
    primary: str = orm.PrimaryKey(str)

    classa_key1: str = orm.Required(str)
    classa_key2: str = orm.Required(str)
    classa_key3: str = orm.Required(str)
    ...

    some_key1: str = orm.Required(str)
    some_key2: str = orm.Required(str)
    ...

class ClassB(db.Entity):
    primary: str = orm.PrimaryKey(str)

    classb_key1: str = orm.Required(str)
    classb_key2: str = orm.Required(str)
    classb_key3: str = orm.Required(str)
    ...

    some_key1: str = orm.Required(str)
    some_key2: str = orm.Required(str)
    ...

into something like this?

class SomeKeyMixin:
    some_key1: str = orm.Required(str)
    some_key2: str = orm.Required(str)
    ...


class ClassA(db.Entity, SomeKeyMixin):
    primary: str = orm.PrimaryKey(str)

    classa_key1: str = orm.Required(str)
    classa_key2: str = orm.Required(str)
    classa_key3: str = orm.Required(str)
    ...


class ClassB(db.Entity, SomeKeyMixin):
    primary: str = orm.PrimaryKey(str)

    classb_key1: str = orm.Required(str)
    classb_key2: str = orm.Required(str)
    classb_key3: str = orm.Required(str)
    ...

Normal inheritance doesn't work because this will put all classes into the same table and this is not desired.

1 Answers
Related