How to convert id into referenced field in Web2py?

无人久伴 提交于 2019-12-12 02:25:56

问题


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.


回答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




回答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!



来源:https://stackoverflow.com/questions/38892120/how-to-convert-id-into-referenced-field-in-web2py

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!