title: SQLAlchemy: get Model from table name. This may imply appending some function to a metaclass constructor as far as I can see
body: <p>I want to make a function that, given the name of a table, returns the model with that <strong>tablename</strong>.
  Eg:</p>
  
  <pre><code>class Model(Base):
      __tablename__ = 'table'
      ...a bunch of Columns
  
  def getModelFromTableName(tablename):
     ...something magical
  </code></pre>
  
  <p>so getModelFromTableName('table') should return the Model class.</p>
  
  <p>My aim is to use the function in a simple form generator I'm making since FormAlchemy does not work with python3.2 and I want it to handle foreign keys nicely. </p>
  
  <p>Can anyone give me any pointers on how to get getModelFromTableName to work?</p>
  
  <p>Here's one idea I have (it might be totally wrong, I haven't worked with meta classes before...)</p>
  
  <p>What if I were to make my Model classes inherit from Base as well as some other class (TableReg) and have the class meta of TableReg store Model.<strong>tablename</strong> in some global dictionary or Singleton. </p>
  
  <p>I realise this could be totally off because Base's metaclass does some very important and totally nifty stuff that I don't want to break, but I assume there has to be a way for me to append a little bit of constructor code to the meta class of my models. Or I don't understand. </p>