I have some code that works but I feel my ignorance is showing...
Using just a table or table-name as input my code compiles a select
statement which includes all the fields in the table plus any referenced
tables.
>>> table.name='entry'
>>> table=eval('db.'+table_name)
>>> field_names=table.fields
>>> print field_names
['id', 'txn_id', 'acc_id', 'debit', 'credit']
>>> fields=[table[field] for field in field_names]
>>> refs=['db.'+table_name+'.'+field.name+' == db.'+field.type[10:]+'.id'
for field in fields if field.type[:9]=='reference']
>>> print refs
['db.entry.txn_id == db.txn.id', 'db.entry.acc_id == db.acc.id']
>>> q=eval('('+') & ('.join(refs)+')')
>>> print q
((entry.txn_id = txn.id) AND (entry.acc_id = acc.id))
>>> s=db(q)
>>> print s
<Set ((entry.txn_id = txn.id) AND (entry.acc_id = acc.id))>
>>> rows=s.select()
>>> print rows[0]
<Row {'acc': {'id': 2L, 'name': 'Bank'}, 'entry': {'acc_id': 2L, 'txn_id':
1L, 'credit': 0.0, 'debit': 456.0, 'id': 54L}, 'txn': {'id': 1L,
'description': 'Opening Balances'}}>
>>>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.