web2py数据库中的表示格式

编程入门 行业动态 更新时间:2024-10-25 08:15:16
本文介绍了web2py数据库中的表示格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

这个格式在这里做什么?

使用格式参数来确定其他表中的字段那将会显示人员表格。例如,如果你定义: $ $ p $ db $ define $'$'$ $ b $ Field $('name'), 字段('owner',db.person)

'所有者'字段是引用人表的参考字段(即,它存储来自人表的记录的记录标识)在大多数情况下,当显示狗表中的数据时,不想显示原始db.person存储在所有者字段中的记录ID,因为它没有任何意义 - 相反,显示人员的名称更有意义。在web2py中,当你创建一个基于'dog'表的SQLFORM时,表格中的属性可以在表单和表格中自动替换。 它会自动为'owner'字段生成一个下拉列表,并且由于'person'表定义的 format ='%(name)s'参数,下拉列表wi将显示db.person名称而不是记录ID(即使在表单提交时,所有者字段将存储相关联的记录ID而不是名称)。

另外,如果在SQLTABLE或SQLFORM.grid中显示dog表中的记录,则owner字段将显示所有者的名称,而不是所有者的记录ID。

请参阅 web2py/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 web2py/books/default/chapter/29/6#Record-representation.

更多推荐

web2py数据库中的表示格式

本文发布于:2023-10-23 14:08:18,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   格式   web2py

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!