将数据库字段的默认值设置为 web2py 中另一个字段的值 [英] Setting the default of a database Field to the value of another field in web2py

查看:23
本文介绍了将数据库字段的默认值设置为 web2py 中另一个字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

db.define_table('mytable',
    Field('start_number', 'double', requires=IS_NOT_EMPTY()),
    Field('current_number', 'double', default=mytable.start_number, requires=IS_NOT_EMPTY()))

当网站上发生某些事情时,current_number 可以更改为其他内容,但始终以 start_number 的默认值开始(即,在创建条目时).每个条目都有不同的start_number.

When something happens on the website, current_number can be changed to something else, but will always start with the default value of start_number (ie, when the entry is created). Each entry has a different start_number.

我不断收到错误消息,说找不到 mytable,可能是因为它还没有完全定义.是否还可以将current_number的默认值设置为start_number?

I keep getting an error saying that mytable can't be found, probably because it hasn't been fully defined yet. Is it still possible to set the default value of current_number to start_number?

推荐答案

如果插入总是通过表单提交完成,你可以这样做:

If inserts will always be done via form submissions, you could do:

Field('current_number', 'double', default=request.post_vars.start_number, ...)

另一种选择是使其成为计算域:

Another option is to make it a computed field:

Field('current_number', 'double', compute=lambda r: r.start_number, ...)

但是,默认情况下,使用 SQLFORM 创建的表单中不显示计算字段,因此如果您想在更新表单中显示该字段,则必须明确指定要显示的字段表单或临时将字段的计算"属性设置为无.

However, by default, computed fields are not shown in forms created with SQLFORM, so if you want to display the field in an update form, you will have to explicitly specify the fields to show on the form or temporarily set the field's "compute" attribute to None.

这篇关于将数据库字段的默认值设置为 web2py 中另一个字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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