在带有引用类型的web2py表字段定义中使用require时,如何解决KeyError?

时间:2018-11-16 04:20:02

标签: web2py

我一直试图在我接管的web2py应用程序的表中设置require属性,以便验证程序将表单中下拉列表的值限制为用户帐户的子集。我已经在Google论坛上阅读了几篇文章,例如https://groups.google.com/forum/#!topic/web2py/n2c3xP6EQ8I,在这里建议可以做到,我已经遵循了这些要求,因此现在在表中具有以下字段定义:

Field('supervisor', type='reference auth_user', label=T('Supervisor'), requires=IS_EMPTY_OR(IS_IN_DB(db((db.auth_user.is_active == True) & (db.auth_user.id == db.auth_membership.user_id) & (db.auth_membership.group_id != 5)), db.auth_user.id, db.auth_user._format))),

尽管运行该应用程序时出现错误消息:

<class 'KeyError'> 'id'

我尝试了其他建议,例如添加:

represent=lambda id, r: '%(first_name)s %(last_name)s' % db.auth_user(id)

到字段定义,但出现相同的错误。

我尝试将查询分为两部分,并在IS_NOT_IN_DB中使用 _and 选项,因为这似乎可能会产生类似的效果:

Field('supervisor', type='reference auth_user', label=T('Supervisor'), requires=IS_EMPTY_OR(IS_IN_DB(db(db.auth_user.is_active == True), db.auth_user.id, db.auth_user._format, _and=IS_NOT_IN_DB(db(db.auth_membership.group_id != 5), db.auth_membership.user_id)))),

尽管上述操作不会产生错误,但也无法正确排除下拉菜单中不需要的用户。我在这里唯一的想法是,可能是因为该应用程序正在使用crud生成表单,而不是显式使用SQLFORM,并且我看到有一篇文章提到Crud本质上已被放弃,而支持SQLFORM ...

作为参考,我的web2py版本是从源代码下载的2.17.2。

有人知道我可以如何修改字段定义以使其正常工作吗?

已更新(2018/11/200):

对不起,这是回溯:

跟踪

    Traceback (most recent call last):
File "C:\Users\Ben\python\web2py\gluon\restricted.py", line 219, in restricted
    exec(ccode, environment)
File "C:/Users/Ben/python/web2py/applications/lycomms/controllers/work_order.py", line 412, in <module>
File "C:\Users\Ben\python\web2py\gluon\globals.py", line 421, in <lambda>
    self._caller = lambda f: f()
File "C:\Users\Ben\python\web2py\gluon\tools.py", line 3867, in f
    return action(*a, **b)
File "C:/Users/Ben/python/web2py/applications/lycomms/controllers/work_order.py", line 187, in add
    form = crud.create(db.work_order)
File "C:\Users\Ben\python\web2py\gluon\tools.py", line 4374, in create
    **attributes
File "C:\Users\Ben\python\web2py\gluon\tools.py", line 4296, in update
    **attributes  # contains hidden
File "C:\Users\Ben\python\web2py\gluon\sqlhtml.py", line 1523, in __init__
    inp = self.widgets.options.widget(field, default)
File "C:\Users\Ben\python\web2py\gluon\sqlhtml.py", line 325, in widget
    options = requires[0].options()
File "C:\Users\Ben\python\web2py\gluon\validators.py", line 2795, in _options
    options = self.other.options(*args, **kwargs)
File "C:\Users\Ben\python\web2py\gluon\validators.py", line 601, in options
    self.build_set()
File "C:\Users\Ben\python\web2py\gluon\validators.py", line 594, in build_set
    self.theset = [str(r[self.kfield]) for r in records]
File "C:\Users\Ben\python\web2py\gluon\validators.py", line 594, in <listcomp>
    self.theset = [str(r[self.kfield]) for r in records]
File "C:\Users\Ben\python\web2py\gluon\packages\dal\pydal\objects.py", line 94, in __getitem__
    raise KeyError(key)
KeyError: 'id'

功能参数列表

(self=<Row {'auth_user': {'id': 39, 'first_name': 'Ben...ship': {'id': 83, 'user_id': 39, 'group_id': 2}}>, k='id')

0 个答案:

没有答案