On Fri, Aug 20, 2010 at 7:39 AM, Eduardo Robles Elvira <edu...@gmail.com>wrote:

> Now, these functions reimplemented in the inherited class models might
> be called by the jobs view. It would be very convenient if directly
> when I get the jobs from the database with sqlalchemy I could directly
> get them in "CustomJobA", "CustomJobB" instances, instead of all being
> instances from the base clas "Job". There's a field in the base Job
> class which tells me which class type it is (basically, it's
> path.to.module and then ClassName).
>
>
SQLAlchemy supports this out of the box.
http://www.sqlalchemy.org/docs/reference/ext/declarative.html#single-table-inheritance

So it will be something like this:

class Job(Base):
    class_name = Column(Unicode(64))
    __mapper_args__ = {'polymorphic_on': class_name}

class CustomJobA(Job):
    __mapper_args__ = {'polymorphic_identity': 'CustomJobA'}

SQLAlchemy will then load correct class instance for you on queries,
depending on the value of the field.

-- 
With best regards,
Daniel Kluev

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to