Dnia 2009-10-30, Pt o godzinie 01:44 -0700, Shane pisze:
> Hi All,
>
> I am using SA within TurboGears. When saving data from form fields, I
> often have a dict of key/value pairs (where values is always a string
> and must by correctly typed) where each key is set up to correspond to
> a column within the SA Table:
> (Data from fields for a ProductLineItem Type)
> kw = {'bol': u'', 'gross_qty': u'', 'id': u'-1', 'item': u'4',
> 'net_qty': u'', 'order_id': u'2', 'ordered_qty': u'8800', 'supplier':
> u'2', 'terminal': u'8'}
>
> I have been manually using setter methods to type-define and save each
> dict value with the correct variable within my LineItem class, but
> this is getting fairly tedious. I wanted a way to just save my entire
> dict using a method within LineItems like:
>
> ======================
> class LineItem(DeclarativeBase):
>
> __tablename__ = 'line_item'
> __table_args__ = {'mysql_engine':mysql_engine_type}
> id = Column(Integer, primary_key=True)
> order_id = Column(Integer, ForeignKey('order.id'))
> ordered_qty = Column(Integer)
> net_qty = Column(Integer)
> item_id = Column(Integer, ForeignKey('item.id'))
> class_type = Column('class_type', String(50))
> __mapper_args__ = {'polymorphic_on':class_type}
>
> ....
> def saveTG(self, kw):
> # Load current classes DB table.
> table = metadata.tables[self.__tablename__]
> for k,v in kw.iteritems():
> if k in dir(self):
> # Get the type of the SA column since
> the value in the class may be a None or
> # undefined.
> field_type = type(table.c[k])
> setattr(self, k, field_type(v))
> ...
>
> ======================
> class ProductLineItem(LineItem):
> TYPE = 'product'
> GROSS = 'gross_qty'
> BOL = 'bol'
> TERMINAL = 'terminal_id'
> SUPPLIER = 'supplier_account_id'
>
> __tablename__ = 'product_line_item'
> __table_args__ = {'mysql_engine':mysql_engine_type}
> id = Column(Integer, ForeignKey('line_item.id'), primary_key = True)
> gross_qty = Column(Integer)
> bol = Column(Unicode(20), nullable=True)
> terminal_id = Column(Integer, ForeignKey('terminal.id'), nullable =
> True)
> supplier_account_id = Column(Integer, ForeignKey
> ('terminal_supplier_account.id'), nullable = True)
> __mapper_args__ = {'polymorphic_identity': 'product'}
>
> Ex:
> line = LineItem()
> line.saveTG(kw)
>
> This works since I am working on the base class, but if I use a
> ProductLineItem
>
> line=ProductLineItem()
> line.saveTG(kw)
>
> If fails since my table contains only the columns for
> ProductLineItem. So how can I get all the columns that a
> ProductLineItem works with? I think i am making this too hard and
> that it is something that should be easy to do.
>
> I appreciate any suggestions!
>
Hi
I think you want mapper properties not table columns?
sqlalchemy.orm.mapper.Mapper.iterate_properties ?
http://www.sqlalchemy.org/docs/05/reference/orm/mapping.html?highlight=iterate_properties#sqlalchemy.orm.mapper.Mapper.iterate_properties
for prop in sqlalchemy.orm.class_mapper( something ).iterate_properties:
print prop.key
Tomasz Jezierski
Tefnet
www.tefnet.pl
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
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
-~----------~----~----~----~------~----~------~--~---