>
> >>> table.name='entry'
>

I assume that should be "table_name".
 

> >>> table=eval('db.'+table_name)
>

No need to use eval -- just do db[table_name].
 

>
> >>> field_names=table.fields
> >>> print field_names
> ['id', 'txn_id', 'acc_id', 'debit', 'credit']
>

No need to store field names -- just use table.fields directly.
 

>
> >>> fields=[table[field] for field in field_names]
>

No need to generate a separate list of the Field objects if all you need it 
for is to iterate over it later -- instead, you can just iterate over the 
Table object, which will yield its Field objects. For example:

[f.type for f in table]

 

>
> >>> 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))
>

No need to build the code as strings and then eval. Instead, you can do:

[db[table_name][field.name] == db[field.type[10:]].id for field in fields
 if field.type[:9] == 'reference']

Actually, the whole thing can be done a little more easily by accessing the 
._references and .referent attributes:

table_name = 'entry'
query = reduce(lambda a, b: a & b,
               (f == f.referent for f in db[table_name]._references))
rows = db(query).select()

Note, if the table might contain any list:reference fields, those will need 
to be filtered out:

table_name = 'entry'
query = reduce(lambda a, b: a & b,
               (f == f.referent for f in db[table_name]._references
                if not f.type.startswith('list:')))
rows = db(query).select()

Anthony

-- 
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.

Reply via email to