Robert Leftwich wrote:
> Warning SQLObject (and Python) newbie question follows:
>
> I have a requirement to display the data from the 'many' side of a
> relationship (e.g. the addresses in a person-address) in an html table,
> where each column in the html table is one row from the 'many' table
> (i.e. each property of the address object is displayed in a new row of
> the same column). I have a working solution but it is not pretty and I
> wonder if there is an efficient SQLObject (or Pythonic way) to do this?
>
> An illustration may help. I need to go from:
>
> Person
> Address1(street, city, state, zip)
> Address2(street, city, state, zip)
> Address3(street, city, state, zip)
> Address4(street, city, state, zip)
>
> to:
>
> Person
> Address1.street Address2.street Address3.street Address4.street
> Address1.city Address2.city Address3.city Address4.city
> Address1.state Address2.state Address3.state Address4.state
> Address1.zip Address2.zip Address3.zip Address4.zip
You'll simply have to do this as a loop, like:
addresses = Person.addresses()
for address in addresses:
print address.street
for address in addresses:
print address.city
etc. You could be fancy and do:
for attr in ['street', 'city', 'state', 'zip']:
for address in addresses:
print getattr(address, attr)
--
Ian Bicking / ia...@co... / http://blog.ianbicking.org
|