getting columns attributes in declarative style with sqlalchemy
Gabriele
hanss at libero.it
Fri Oct 28 13:03:35 EDT 2011
Hi,
I'm tryed to write my first application using SqlAlchemy. I'm using
declarative style. I need to get the attributes of the columns of my
table. This is an example of my very simple model-class:
class Country(base):
__tablename__ = "bookings_countries"
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
canceled = sqlalchemy.Column(sqlalchemy.Boolean, nullable=False,
default=False)
description = sqlalchemy.Column(Description)
def __init__(self, canceled, description):
self.canceled = canceled
self.description = description
def __repr__(self):
return "<Country ('%s)>" % (self.description)
I want to populate a wx grid using the name of the fields as labels of
the columns.
I found three different solutions of my problem, but none satisfeid
me:
a) using column_descriptions in query object.
col = (model.Country.canceled, model.Country.description)
q =
self.parent.session.query(*col).order_by(model.Country.description)
l = [column["name"] for column in q.column_descriptions]
in this way, l contains exactly what I need, but I don't like because
I must execute an useless query only for having informations about the
structure of my table. Ok, it's not so big problem, but IMO is not a
very good solution
b) reflecting the table
c) using inspector lib from sqlachemy.engine
I don't like because I have to use directly the name of the table in
the database...
It sounds me better and logical to use my class Country... but I can't
find the simple way for doing that... maybe it's very simple, but...
Someone can help me?
Thanks
Gabriele
More information about the Python-list
mailing list