On Aug 19, 2010, at 10:39 PM, Eduardo Robles Elvira wrote: > Hello everyone: > > This is my first post in this mailing list/group. My question is > simple: is there some way to tell sqlalchemy which method should be > called to instance models? Now, some background: > > I'm working in a project with plugins support. In the project core > I've got a "Job" class model. There's also a model view (I'm using > web.py) which shows information about the jobs. > > Plugins can extend the Jobs, inheriting from them. They won't need to > add any new column to the model, just reimplement some base functions > for convenience. So I might have the class CustomJob which inherits > the model class Job. > > 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). > > I can define a factory method that given a base job instance, or > something similar, can return an instance of the correct model. The > simplest way to work around this problem is to put that function as a > member function called def instance(self): inside the base Job class, > and then anyone who needs it calls job.instance() to get an instance > of the needed class, but it's a bit troublesome. I'd like it to be > more transparent. The job instance should be given to me directly of > the class type needed. > > So the question I mentioned earlier remains: is there some way to tell > sqlalchemy which method should be called to instance models? I've seen > http://www.sqlalchemy.org/docs/reference/ext/declarative.html#inheritance-configuration > and > http://www.sqlalchemy.org/docs/reference/ext/declarative.html#class-constructor > but there doesn't seem to be a straightforward to do what I need.
It does. Your type-column is the discriminator, and you need to give it a discriminator. You can use some metaclass-magic to have that derived automagically from the classname itself. AFAIK Elixir does that, there you neither declare the discriminator-column nor the value for it by hand. Diez -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
