【问题标题】:web2py and column aliasingweb2py 和列别名
【发布时间】:2014-02-05 13:21:45
【问题描述】:

我在控制器中有这样的查询(很多列都被注释掉了)

rows=db( (c.email_sent == 0 ) & (c.status=='pending') ).select(db.f.first_name #.with_alias('requester_name') ,
# db.t.first_name.with_alias('receiver_name'),db.t.email.with_alias('sendto_email'),
# db.c.from_id.with_alias('sender_id'),db.c.to_id.with_alias('receiver_id'),
# db.c.id.with_alias('connection_id')
,join = 
[db.f.on (db.f.id==c.from_id),
db.t.on(db.t.id==c.to_id)]
    )

视图有

{{extend 'layout.html'}}
<table class="table">
    {{for row in rows:}}
    <tr>
        <td>={{row.first_name}</td>
    </tr>
</table>

而且效果很好。但是,如果我从控制器中删除注释掉的别名,我就无法让视图工作。我收到一个错误 - “(Rows 对象中有问题)”
函数参数列表为 (self=, sqlrows=, linkto=None, upload=None, orderby=None, headers={}, truncate=16, columns=['f.first_name AS requester_name', 't.first_name AS receiver_name ', 't.email AS sendto_email', 'c.from_id AS sender_id', 'c.to_id AS receiver_id', 'c.id AS connection_id'], th_link='', extracolumns=None, selectid=None, renderstyle=假,cid=无,colgroup=假,**属性={}) 显然,列名没有正确传递。这应该怎么做?

【问题讨论】:

    标签: sql web2py


    【解决方案1】:

    如果您为某个字段设置别名,则在访问该行中的该字段时使用该别名:

    {{=row.requester_name}}
    

    另一种选择是跳过使用别名——它们似乎没有必要。在这种情况下,由于查询涉及连接,您可以通过 row.tablename.fieldname 访问各个字段值。

    【讨论】:

    • row.tablename.fieldname 没有帮助(使用别名时)。这是打印行显示的内容 -
    • 好的,我在控制器中的返回语法错误(我是新手)。它必须是返回字典(行=行)。我在做返回行
    • 注意,我建议使用row.tablename.fieldname 语法,前提是您使用别名。
    猜你喜欢
    • 2011-11-22
    • 1970-01-01
    • 2011-01-19
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多