Change __tablename__ at runtime?

554 views
Skip to first unread message

Wade Leftwich

unread,
Jul 19, 2010, 1:56:49 PM7/19/10
to sqlalchemy
I've been using Sqlalchemy to help migrate a bunch of websites into
and out of Drupal. Since a Drupal 'node' can involve fields from 10 or
12 tables, the Declarative approach has been a real timesaver.

But now they're thrown me a serious curveball. It turns out that
Drupal has a 'multisite' mode, where instead of one `node` table you
have `site1_node`, `site2_node`, etc.

I'm not going to try to do a union of `site1_node` and `site2_node` or
anything like that, but -- given that they have exactly the same
structure, is there any way I can define a Node class and specify the
__tablename__ during runtime?

Any advice appreciated; I'm prepared for the advice to be "Don't go
there".


Wade Leftwich
Ithaca, NY

Michael Bayer

unread,
Jul 19, 2010, 3:53:58 PM7/19/10
to sqlal...@googlegroups.com

you'd probably want to use a variant of the entity name recipe, I've added a declarative example at the end:

http://www.sqlalchemy.org/trac/wiki/UsageRecipes/EntityName

when you create an instance of a class, you need to know what table you're going to want to be going to.


Reply all
Reply to author
Forward
0 new messages