On Wednesday, 25 July 2012 22:32:34 UTC+1, Michael Bayer wrote:
>
>
> On Jul 25, 2012, at 4:29 PM, Stephan Hügel wrote:
>
>
> OK, I've done the following
>
> def create_signlist(sl):
> class SignList(db.Model):
>
> __tablename__ = listname.lower()
>
> id = db.Column("id", db.Integer(), primary_key=True)
> reference = db.Column(db.String(50), nullable=False, unique=True)
>
> def __init__(self, reference):
> self.reference = reference
>
> SignList.__name__ = listname
> return SignList
>
> signlists = ['lka', 'kal']
> for s in signlists:
> create_signlists(s)
>
> But this gives me a warning:
>
> SAWarning: The classname 'SignList' is already in the registry of this
> declarative base, mapped to <class 'glyph.models.lka'>
> _as_declarative(cls, classname, cls.__dict__)
>
>
> that's only a warning, you can ignore it. It means if you have some
> class which makes a relationship() to "SignList" using just the name, you
> won't get this new class.
>
> if you want to make a new class that has a new name from the start, use
> type():
>
> def __init__(self, reference):
> self.reference = reference
>
> d = dict(
> __tablename__ = listname.lower()
>
> id = db.Column("id", db.Integer(), primary_key=True)
> reference = db.Column(db.String(50), nullable=False, unique=True)
>
> __init__ = __init__
> )
> my_class = type(listname, (db.Model,), d)
>
>
>
Doing this creates classes (e.g. <class 'flask_sqlalchemy.lak'>) , but is
there some way for me to instantiate them without resorting to some eval magic
in my for loop? What I'd like to end up with is programmatically created
instances (myapp.models.lak) that I can use the same way as my "normally"
instantiated classes.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sqlalchemy/-/kN0EM1lgC8IJ.
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.