Setting the default of a database Field to the value of another field in web2py

耗尽温柔 提交于 2019-12-06 16:01:50

问题


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

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.

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?


回答1:


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, ...)

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.



来源:https://stackoverflow.com/questions/16280220/setting-the-default-of-a-database-field-to-the-value-of-another-field-in-web2py

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!