On Wednesday, 25 July 2012 23:52:18 UTC+1, Michael Bayer wrote:
>
>
> instantiate, as in, instance of the new class?  "my_class" above is a 
> regular Python class,  just instantiate - myclass(x, y, z).   
>
>
> 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.
>
>
> that's what type() gives you.  Its the exact same thing as using "class 
> Foo()".
>
>

Hmm. I'm explaining this badly.

from myapp import db


class Foo(db.Model):
    __tablename__ = 'foo'
    id = db.Column(db.Integer, primary_key=True)
    othercol = db.Column(db.String(50), nullable=False, unique=True)
    
# I can import this, and immediately run a query like Foo.query.all()
# no manual instantiation required


def create_signlist(listname):
    def __init__(self, reference=None):
        self.reference = reference
    d = dict(
        __tablename__ = listname.lower(),
        __table_args__ = {'mysql_engine': 'InnoDB'},
        __mapper_args__ = {'always_refresh': True},
        id = db.Column("id", db.Integer(), primary_key=True),
        reference = db.Column(db.String(50), nullable=False, unique=True),
        __init__ = __init__
    )
    cls = type(listname, (db.Model,), d)
    return cls()

various = ['Abc', 'Def', 'Ghi']
for v in various:
    v = create_signlist(v)

# what do I have to do now to end up with instances Abc, Def, Ghi that I 
can query within my app in the same way as Foo?
 

-- 
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/-/9vSrN_gcjwUJ.
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.

Reply via email to