Skip to content Skip to sidebar Skip to footer

How To Convert Id Into Referenced Field In Web2py?

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', '

Solution 1:

You need to use format: Record representation correctly and use render() to convert ids into their respective representation.

Your model will look like this:

db.define_table('A',
                Field('A1', 'string', required=True),
                Field('A2', 'string', required=True),
                Field('A3', 'string', required=True),
                format='%(A1)s, %(A2)s')

db.define_table('B',
                Field('B1', 'string', required=True),
                Field('B2', 'string', required=True),
                Field('B3', 'string', required=True),
                format='%(B1)s, %(B2)s')

db.define_table('C',
                Field('C1', db.A),
                Field('C2', db.B),
                Field('C3', 'string', required=True))

And update controller and use render(). render() returns a generator to iterate over all rows.

def C_view():
    if request.args(0) is None:
        rows = db(db.C).select(orderby=db.C.C1|db.C.C2).render()
    else:
        letter = request.args(0)
        rows = db(db.C.C1.startswith(letter)).select(orderby=db.C.C1|db.C.C2).render()
    return locals()

Reference:

Rendering rows using represent

Format: Record representation

Solution 2:

The web2py syntax to define reference fields is the following, as far as I know:

Field('C1', 'reference A'),
Field('C2', 'reference B'),

Then, in your view, x.C1 will be a Row object from the A table, so:

<td>{{=x.C1.A1}}, {{=x.C1.A2}}, {{=x.C1.A3}}</td><td>{{=x.C2.B1}}, {{=x.C2.B2}}, {{=x.C2.B3}}</td>

Hope it helps!

Post a Comment for "How To Convert Id Into Referenced Field In Web2py?"