Just started learning flask and python and I had to create 10+ tables(MySQLAlchemy) with the same structure and constraints. so after looking aroud for a quicker solution than declaring each class separately, I did the following in models/user.py:
#models/users.py
class exchangeapis(object):
email = db.Column(db.String(50), primary_key=True)
api =db.Column(db.String(100))
secret= db.Column(db.String(100))
@declared_attr.cascading 
# Makes sure all the dbs derived from this calss get the fK and PK
def email(cls):
        #this is necessary to propagate fk and pk to the instances
        return db.Column(db.String(50), db.ForeignKey('users.email'), primary_key=True)
#general methods to get class data       
def get_api(self):
    return self.api
def get_secret(self):
    return self.secret
exchange=['A','B','C']    
for exchange in exchanges:
cls=type(exchange.title(), (exchangeapis, db.Model), { '__tablename__' : str(exchange)+"_api"})
print(cls.__name__)
the
print(cls.__name__) 
gives me 'A', 'B', 'C' which I expect to be 'A_api', 'B_api'...
when I try to import it into my account/views.py file, with the following:
from ..models import User, A_api
I get " File "/home/puru991/flask-saas/flask-base/app/account/views.py", line 8, in from ..models import User,A_api
ImportError: cannot import name 'A_api'"
but if i define the class in the following way:
class A_api(db.Model):
__tablename__="A_api"
email = db.Column(db.String(50),ForeignKey("users.email"), primary_key=True)
api =db.Column(db.String(100))
secret= db.Column(db.String(100))
#general methods to get class data       
def get_api(self):
    return self.api
def get_secret(self):
    return self.secret
There are no errors. So my questions is how do I dynamically create classes based on exchangeapi(object) and import them successfully? I went through about 8-10 answers around here and the most i could learn was how to create them dynamically. I also learnt that MySQLAlchemy creates classes with the same name as the tablename, so I thought referring by the table name(which is "A_api") would do the trick. What am I missing?


