python - web2py,一对多,非必需关系

标签 python web2py

考虑 web2py python 网络框架中的 2 个表。

默认auth_user

还有这个:

b.define_table(
    'artwork',
    Field('user_id', 'reference auth_user', required=False),
    Field('name'),
    Field('description', 'text')
    )

现在,当我转到 (appadmin) 中的数据库管理时,我希望 user_id 是可选的。如果我让选择下拉为空,当在艺术品表中手动输入一个条目时,它会说:“值不在数据库中”与 required=False 语句相吻合。

我希望能够在没有 user_id 的情况下插入艺术品条目

谁能帮我解决这个问题?非常感谢

最佳答案

当您创建引用字段时,默认情况下它会获得一个 requires=IS_IN_DB(...) 验证器。 requires 属性在表单级别强制执行,而 required 属性在 DAL 级别强制执行。要覆盖默认表单验证器,您可以执行以下操作:

Field('user_id', 'reference auth_user', requires=None)

或者,

Field('user_id', 'reference auth_user',
      requires=IS_EMPTY_OR(IS_IN_DB(db, 'auth_user.id',
                                    db.auth_user._format)))

关于python - web2py,一对多,非必需关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21272794/

相关文章:

python - Google 图片 APIexecute_transforms() 返回 "there has been an error"

python - Gunicorn 和 Supervisor 错误文件没有可执行权限

python - web2py 外部库

python - web2py表单流程

python - web2py下载失败时如何在 View 中显示Flash错误消息?

python - 属性错误: 'Settings' object has no attribute 'TEMPLATE_CONTEXT_PROCESSORS'

python - 如何滚动 Tix ScrolledWindow

python - 请求多值参数的url编码

python - 修改Web2py中sqlform.grid()的列输出

python - 如何从单个查询中检查多个日期的冲突