On Wed, Sep 09, 2009 at 10:14 -0700, gizli wrote:
> This list has been very helpful so far, thanks a lot. I was just
> wondering if there is a transparent way to assign polymorphic
> identities to ORM classes using single table inheritance. Let's say we
> have a base Task class:
>
> class Task(DeclarativeBase):
> __tablename__ = 'Tasks'
>
> id = Column(Integer)
> name = Column(String)
> type = Column(String) etc...
>
> __mapper_args__ = {'polymorphic_on' : type}
>
> class MyTask(Task):
> __mapper_args__ = {'polymorphic_identity': 'MyTask'}
>
> class YourTask(Task):
> __mapper_args__ = {'polymorphic_identity': 'YourTask'}
>
> This is the recommended way in the documentation for declarative
> style. I was wondering if I could get rid of the explicit
> "polymorphic_identity" setup in the subclasses by some clever
> programming trick. I want to assign the polymorphic identity to be the
> class __name__ automatically if that class extends Task class. I am
> not very well versed in advanced python programming like decorators or
> function/class wrappers, so I wanted to seek your opinion.
Maybe something like:
class Task(DeclarativeBase):
__tablename__ = 'Tasks'
id = Column(Integer)
name = Column(String)
type = Column(String) etc...
__mapper_args__ = {
'polymorphic_on' : type,
'polymorphic_identity : self.__class__.__name__,
}
class MyTask(Task):
pass
I have no idea if that works, but you could try it.
with kind regards
Wolodja Wentland
signature.asc
Description: Digital signature
