web2py 数据库中的表示格式 [英] representation format in web2py database

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

问题描述

<块引用><块引用><块引用>

db.define_table('person', Field('name'), format='%(name)s')

这种格式在这里有什么作用?

解决方案

format 参数用于确定引用person"表的其他表中的字段将如何显示.例如,如果您定义:

db.define_table('dog',字段('名称'),字段('所有者',db.person)

所有者"字段是引用人员"表的引用字段(即,它存储人员"表中记录的记录 ID).在大多数情况下,当您显示狗"表中的数据时,您不想显示存储在所有者"字段中的原始 db.person 记录 ID,因为这没有任何意义——相反,显示此人的姓名"更有意义.在 web2py 中,表格的 format 属性可以在表格和表格中自动替换.

当你创建一个基于'dog'表的SQLFORM时,它会自动为'owner'字段生成一个下拉列表,并且由于format='%(name)s' 参数,下拉列表将显示 db.person 名称而不是记录 id(即使在提交表单时,'owner' 字段将存储关联的记录 id 而不是名称).

此外,如果您在 SQLTABLE 或 SQLFORM.grid 中显示狗"表中的记录,所有者"字段将显示所有者的姓名而不是所有者的记录 ID.

参见 http://web2py.com/books/default/chapter/29/6#Record-representation.

db.define_table('person', Field('name'), format='%(name)s')

What does this format do here?

解决方案

The format argument is used to determine how fields in other tables that reference the 'person' table will be displayed. For example, if you define:

db.define_table('dog',
    Field('name'),
    Field('owner', db.person)

The 'owner' field is a reference field that references the 'person' table (i.e., it stores record id's of records from the 'person' table). In most cases, when you display data from the 'dog' table, you don't want to display the raw db.person record id that is stored in the 'owner' field because that doesn't have any meaning -- instead, it makes more sense to display the 'name' of the person. In web2py, the format attribute of the table enables this automatic substitution in both forms and tables.

When you create a SQLFORM based on the 'dog' table, it will automatically generate a drop-down list for the 'owner' field, and because of the format='%(name)s' argument to the 'person' table definition, the drop-down list will display db.person names instead of record id's (even though upon form submission, the 'owner' field will store the associated record id rather than the name).

Also, if you display records from the 'dog' table in a SQLTABLE or SQLFORM.grid, the 'owner' field will show the owner's name rather than the owner's record id.

See http://web2py.com/books/default/chapter/29/6#Record-representation.

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

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