Consider the 3 tables below (A
, B
& C
) where table C
has 2 fields referenced to table A
and B
.
Model:
db.define_table('A',
Field('A1', 'string', required =True),
Field('A2', 'string', required =True),
Field('A3', 'string', required =True),
format=lambda r: '%s, %s' % (r.A.A1, r.A.A2))
db.define_table('B',
Field('B1', 'string', required=True),
Field('B2', 'string', required=True),
Field('B3', 'string', required=True),
format=lambda r: '%s, %s' % (r.B.B1, r.B.B2))
db.define_table('C',
Field('C1', db.A),
Field('C2', db.B),
Field('C3', 'string', required=True),
format=lambda r: '%s, %s - %s' % (r.C.C1, r.C.C2))
Controller:
def C_view():
if request.args(0) is None:
rows = db(db.C).select(orderby=db.C.C1|db.C.C2)
else:
letter = request.args(0)
rows = db(db.C.C1.startswith(letter)).select(orderby=db.C.C1|db.C.C2)
return locals()
In the corresponding view below I display the 3 fields of table C:
...
{{ for x in rows:}}
<tr>
<td>{{=x.C1}}</td>
<td>{{=x.C2}}</td>
<td>{{=x.C3}}</td>
</tr>
{{pass}}
...
With this setup, the view displays the foreign id of C1 & C2. How would I have to modify the model, controller and/or the view to display the corresponding reference fields rather than the id's? In other words:
for C1
the view should display r.A.A1, r.A.A2
and for C2
the view should display r.B.B1, r.B.B2
.
Thank you.