在发送查看之前修改 web2py 中的 CRUD 表单
我似乎无法找到一种方法来修改通过以下方式创建的表单:
from gluon.tools import Crud
crud = Crud(globals(), db)
form = crud.create(db.table_name)
由于我在表中使用外键,自动生成的表单仅允许整数(代表外主键),但我想要能够做的就是输入外部数据字段所需的任何数据类型(而不仅仅是标识符)。有没有一种简单的方法来告诉create()函数使用外部表的数据类型而不是主表的数据类型(即自动递增主键)?
I cannot seem to find a way to modify a form that has been created via:
from gluon.tools import Crud
crud = Crud(globals(), db)
form = crud.create(db.table_name)
Since I am using foreign keys in my table, the auto-generated form only allows an integer (which represents the foreign primary key), but what I want to be able to do is enter whatever data type the foreign data field requires (rather than just the identifier). Is there an easy way to tell the create() function to use the foreign table's data type rather than the primary table's data type (which is the auto-incrementing primary key)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用数据库验证器。
它将显示一个选择框,其中包含外部表中的值:
(来自 http://web2py.com/book/default/ Chapter/07?search=requires#Database-Validators):
IS_IN_DB
考虑以下表格和要求:
它在狗 INSERT/UPDATE/DELETE 表单级别强制执行。它要求dog.owner是数据库db中的person.id字段中的有效ID。由于这个验证器,dog.owner 字段被表示为一个保管箱。验证器的第三个参数是一个描述保管箱中元素的字符串。在示例中,您希望查看人员 %(name)s 而不是人员 %(id)s。 %(...)s 替换为每条记录括号中字段的值。
零选项的工作方式与
IS_IN_SET
验证器非常相似。如果您希望验证该字段,但不需要保管箱,则必须将验证器放入列表中。
验证器的第一个参数可以是数据库连接或 DAL 集,如
IS_NOT_IN_DB
中所示。有时您需要下拉框(因此您不想使用上面的列表语法)但又想使用其他验证器。为此,IS_IN_DB 验证器需要一个额外的参数 _and ,如果验证的值通过了 IS_IN_DB 验证,则该参数可以指向所应用的其他验证器的列表。例如,验证数据库中不属于子集的所有狗主人:
IS_IN_DB 和标记
IS_IN_DB 验证器有一个可选属性 multiple=False。如果设置为 True,可以在一个字段中存储多个值。该字段应该是 list:reference 类型,如第 6 章中讨论的那样。其中讨论了标记的显式示例。多个引用在创建和更新表单中自动处理,但它们对 DAL 是透明的。我们强烈建议使用 jQuery 多选插件来渲染多个字段。
You can use database validators to it.
It will show a select box with the values from foreign table:
(from http://web2py.com/book/default/chapter/07?search=requires#Database-Validators):
IS_IN_DB
Consider the following tables and requirement:
It is enforced at the level of dog INSERT/UPDATE/DELETE forms. It requires that a dog.owner be a valid id in the field person.id in the database db. Because of this validator, the dog.owner field is represented as a dropbox. The third argument of the validator is a string that describes the elements in the dropbox. In the example you want to see the person %(name)s instead of the person %(id)s. %(...)s is replaced by the value of the field in brackets for each record.
The zero option works very much like for the
IS_IN_SET
validator.If you want the field validated, but you do not want a dropbox, you must put the validator in a list.
The first argument of the validator can be a database connection or a DAL Set, as in
IS_NOT_IN_DB
.Occasionally you want the drop-box (so you do not want to use the list syntax above) yet you want to use additional validators. For this purpose the IS_IN_DB validator takes an extra argument _and that can point to a list of other validators applied if the validated value passes the IS_IN_DB validation. For example to validate all dog owners in db that are not in a subset:
IS_IN_DB and Tagging
The IS_IN_DB validator has an optional attribute multiple=False. If set to True multiple values can be stored in one field. This field should be of type list:reference as discussed in Chapter 6. An explicit example of tagging is discussed there. multiple references are handled automatically in create and update forms, but they are transparent to the DAL. We strongly suggest using the jQuery multiselect plugin to render multiple fields.