子孙中的web2py表示形式“格式" [英] web2py representation 'format' in grandchild

查看:94
本文介绍了子孙中的web2py表示形式“格式"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题解答(和文档)描述了define_table函数的format=参数的使用.我在使它在稍微复杂一些的情况下工作时遇到问题.我正在使用web2py版本(1, 99, 7, datetime.datetime(2012, 3, 4, 22, 12, 8), 'stable')和Python 2.5.4. (我使用的是MySQL,但我认为这无关紧要.)

The q&a here (and the doc) describe the use of the format= argument to the define_table function. I have a problem getting that to work in a slightly more complicated case. I'm using web2py version (1, 99, 7, datetime.datetime(2012, 3, 4, 22, 12, 8), 'stable'), and Python 2.5.4. (I'm using MySQL, but I assume that's irrelevant.)

我有以下内容:

独立"(不是子级)表InstitutionPerson.表TeamInstitution的子级.表Team_staffPersonTeam连接在一起.这是修剪后的版本:

'Independent' (not a child) tables Institution and Person. Table Team is a child of Institution. Table Team_staff connects Person and Team together. Here's a trimmed version:

db.define_table('Person',
                Field('First_name', 'string', length=60, required=True),
                Field('Last_name', 'string', length=60, required=True),
                ...
                format='%(First_name)s %(Last_name)s')

db.define_table('Institution',
                Field('Institution_name', 'string', length=60, required=True,
                   unique=True),
                format='%(Institution_name)s')

db.define_table('Team',
                Field('Institution', db.Institution),
                Field('Sex', 'string', required=True,
                   requires=IS_IN_SET(['m', 'f'])),
                Field('Level', 'string', required=True),
                ...
                format='%(Institution)s %(Sex)s')

db.define_table('Team_staff',
                Field('Team', db.Team),
                Field('Team_staff_member', db.Person),
                ...
                Field('Team_position', 'string', required=True))

到目前为止,太好了.我有一个创建SQLFORM(db.Team_staff)的控制器,以及一个仅显示表单的视图.当我下拉团队"下拉列表时,我看到机构 id 与性别"值(例如1 f,然后是其下的1 m,然后是2 f,等等)串联在一起向前).由于Institution具有format='%(Institution_name)s',为什么我看不到机构名称而不是机构ID?

So far, so good. I have a controller that creates a SQLFORM(db.Team_staff), and a view that simply displays the form. When I drop down the 'Team' dropdown, I see the Institution id concatenated with the 'sex' value (such as 1 f, then 1 m below that, then 2 f, and so forth). As Institution has format='%(Institution_name)s', why am I not seeing the institution name instead of the institution id?

推荐答案

"format"属性不会以这种方式在表之间传播.相反,您应该能够将格式"属性定义为lambda函数,该函数将表的一行作为参数:

The "format" attribute does not propagate across tables that way. Instead, you should be able to define the "format" attribute as a lambda function, which takes a row of the table as its argument:

db.define_table('Team', ...,
    format=lambda r: '%s %s' % (db.Institution[r.Institution].Institution_name,
                                r.Sex))

这篇关于子孙中的web2py表示形式“格式"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆