How can I fix KeyError when using requires in web2py Table Field definition with reference type?












0















I've been trying to set the requires property in a table in a web2py application I've taken over so that the validator will limit the values for the drop-down in the form to a subset of the user accounts. I've read a couple of posts on Google forums, eg https://groups.google.com/forum/#!topic/web2py/n2c3xP6EQ8I, and here suggesting it can be done and I've followed those such that I now have the following field definition in the table:



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


When I run the application though I get an error that says:



<class 'KeyError'> 'id'


I have tried other suggestions like adding:



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


to the Field definition but it comes up with the same error.



I've tried breaking the query into two and using the _and option with IS_NOT_IN_DB since it seemed this might give a similar effect:



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


Whilst the above doesn't generate an error it also doesn't exclude the unwanted users in the drop-down correctly either. The only thought I had here was that it might be because the the application is using crud to generate the forms instead of explicitly using SQLFORM and a post I saw mentioned that Crud has essentially been dropped in favour of SQLFORM...



For reference my web2py version is 2.17.2 downloaded from source.



Does anyone know how I might fix the field definition to make this work?



UPDATED (2018/11/200):



Sorry, here's the traceback:



Traceback



    Traceback (most recent call last):
File "C:UsersBenpythonweb2pygluonrestricted.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:UsersBenpythonweb2pygluonglobals.py", line 421, in <lambda>
self._caller = lambda f: f()
File "C:UsersBenpythonweb2pygluontools.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:UsersBenpythonweb2pygluontools.py", line 4374, in create
**attributes
File "C:UsersBenpythonweb2pygluontools.py", line 4296, in update
**attributes # contains hidden
File "C:UsersBenpythonweb2pygluonsqlhtml.py", line 1523, in __init__
inp = self.widgets.options.widget(field, default)
File "C:UsersBenpythonweb2pygluonsqlhtml.py", line 325, in widget
options = requires[0].options()
File "C:UsersBenpythonweb2pygluonvalidators.py", line 2795, in _options
options = self.other.options(*args, **kwargs)
File "C:UsersBenpythonweb2pygluonvalidators.py", line 601, in options
self.build_set()
File "C:UsersBenpythonweb2pygluonvalidators.py", line 594, in build_set
self.theset = [str(r[self.kfield]) for r in records]
File "C:UsersBenpythonweb2pygluonvalidators.py", line 594, in <listcomp>
self.theset = [str(r[self.kfield]) for r in records]
File "C:UsersBenpythonweb2pygluonpackagesdalpydalobjects.py", line 94, in __getitem__
raise KeyError(key)
KeyError: 'id'


Function Argument List



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









share|improve this question

























  • Always provide the full traceback.

    – Anthony
    Nov 16 '18 at 13:59











  • Thanks for your comment Anthony. I've updated the question with the traceback and I included the Function Argument List part as well in case it helps.

    – bstredinnick
    Nov 20 '18 at 3:51











  • Actually, using the exact code you provided in the first line above, I cannot reproduce the error. I suggest you post on the Google Group and maybe attach a minimal app that reproduces the problem.

    – Anthony
    Nov 26 '18 at 15:08













  • Ok, thanks for your help Anthony I appreciate it. The app is pretty old so there could be something else there that's breaking it. A simpler reference works without a problem though so for now I'll just add another column to auth_user as a work around and come back to it later.

    – bstredinnick
    Nov 26 '18 at 23:29
















0















I've been trying to set the requires property in a table in a web2py application I've taken over so that the validator will limit the values for the drop-down in the form to a subset of the user accounts. I've read a couple of posts on Google forums, eg https://groups.google.com/forum/#!topic/web2py/n2c3xP6EQ8I, and here suggesting it can be done and I've followed those such that I now have the following field definition in the table:



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


When I run the application though I get an error that says:



<class 'KeyError'> 'id'


I have tried other suggestions like adding:



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


to the Field definition but it comes up with the same error.



I've tried breaking the query into two and using the _and option with IS_NOT_IN_DB since it seemed this might give a similar effect:



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


Whilst the above doesn't generate an error it also doesn't exclude the unwanted users in the drop-down correctly either. The only thought I had here was that it might be because the the application is using crud to generate the forms instead of explicitly using SQLFORM and a post I saw mentioned that Crud has essentially been dropped in favour of SQLFORM...



For reference my web2py version is 2.17.2 downloaded from source.



Does anyone know how I might fix the field definition to make this work?



UPDATED (2018/11/200):



Sorry, here's the traceback:



Traceback



    Traceback (most recent call last):
File "C:UsersBenpythonweb2pygluonrestricted.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:UsersBenpythonweb2pygluonglobals.py", line 421, in <lambda>
self._caller = lambda f: f()
File "C:UsersBenpythonweb2pygluontools.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:UsersBenpythonweb2pygluontools.py", line 4374, in create
**attributes
File "C:UsersBenpythonweb2pygluontools.py", line 4296, in update
**attributes # contains hidden
File "C:UsersBenpythonweb2pygluonsqlhtml.py", line 1523, in __init__
inp = self.widgets.options.widget(field, default)
File "C:UsersBenpythonweb2pygluonsqlhtml.py", line 325, in widget
options = requires[0].options()
File "C:UsersBenpythonweb2pygluonvalidators.py", line 2795, in _options
options = self.other.options(*args, **kwargs)
File "C:UsersBenpythonweb2pygluonvalidators.py", line 601, in options
self.build_set()
File "C:UsersBenpythonweb2pygluonvalidators.py", line 594, in build_set
self.theset = [str(r[self.kfield]) for r in records]
File "C:UsersBenpythonweb2pygluonvalidators.py", line 594, in <listcomp>
self.theset = [str(r[self.kfield]) for r in records]
File "C:UsersBenpythonweb2pygluonpackagesdalpydalobjects.py", line 94, in __getitem__
raise KeyError(key)
KeyError: 'id'


Function Argument List



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









share|improve this question

























  • Always provide the full traceback.

    – Anthony
    Nov 16 '18 at 13:59











  • Thanks for your comment Anthony. I've updated the question with the traceback and I included the Function Argument List part as well in case it helps.

    – bstredinnick
    Nov 20 '18 at 3:51











  • Actually, using the exact code you provided in the first line above, I cannot reproduce the error. I suggest you post on the Google Group and maybe attach a minimal app that reproduces the problem.

    – Anthony
    Nov 26 '18 at 15:08













  • Ok, thanks for your help Anthony I appreciate it. The app is pretty old so there could be something else there that's breaking it. A simpler reference works without a problem though so for now I'll just add another column to auth_user as a work around and come back to it later.

    – bstredinnick
    Nov 26 '18 at 23:29














0












0








0








I've been trying to set the requires property in a table in a web2py application I've taken over so that the validator will limit the values for the drop-down in the form to a subset of the user accounts. I've read a couple of posts on Google forums, eg https://groups.google.com/forum/#!topic/web2py/n2c3xP6EQ8I, and here suggesting it can be done and I've followed those such that I now have the following field definition in the table:



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


When I run the application though I get an error that says:



<class 'KeyError'> 'id'


I have tried other suggestions like adding:



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


to the Field definition but it comes up with the same error.



I've tried breaking the query into two and using the _and option with IS_NOT_IN_DB since it seemed this might give a similar effect:



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


Whilst the above doesn't generate an error it also doesn't exclude the unwanted users in the drop-down correctly either. The only thought I had here was that it might be because the the application is using crud to generate the forms instead of explicitly using SQLFORM and a post I saw mentioned that Crud has essentially been dropped in favour of SQLFORM...



For reference my web2py version is 2.17.2 downloaded from source.



Does anyone know how I might fix the field definition to make this work?



UPDATED (2018/11/200):



Sorry, here's the traceback:



Traceback



    Traceback (most recent call last):
File "C:UsersBenpythonweb2pygluonrestricted.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:UsersBenpythonweb2pygluonglobals.py", line 421, in <lambda>
self._caller = lambda f: f()
File "C:UsersBenpythonweb2pygluontools.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:UsersBenpythonweb2pygluontools.py", line 4374, in create
**attributes
File "C:UsersBenpythonweb2pygluontools.py", line 4296, in update
**attributes # contains hidden
File "C:UsersBenpythonweb2pygluonsqlhtml.py", line 1523, in __init__
inp = self.widgets.options.widget(field, default)
File "C:UsersBenpythonweb2pygluonsqlhtml.py", line 325, in widget
options = requires[0].options()
File "C:UsersBenpythonweb2pygluonvalidators.py", line 2795, in _options
options = self.other.options(*args, **kwargs)
File "C:UsersBenpythonweb2pygluonvalidators.py", line 601, in options
self.build_set()
File "C:UsersBenpythonweb2pygluonvalidators.py", line 594, in build_set
self.theset = [str(r[self.kfield]) for r in records]
File "C:UsersBenpythonweb2pygluonvalidators.py", line 594, in <listcomp>
self.theset = [str(r[self.kfield]) for r in records]
File "C:UsersBenpythonweb2pygluonpackagesdalpydalobjects.py", line 94, in __getitem__
raise KeyError(key)
KeyError: 'id'


Function Argument List



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









share|improve this question
















I've been trying to set the requires property in a table in a web2py application I've taken over so that the validator will limit the values for the drop-down in the form to a subset of the user accounts. I've read a couple of posts on Google forums, eg https://groups.google.com/forum/#!topic/web2py/n2c3xP6EQ8I, and here suggesting it can be done and I've followed those such that I now have the following field definition in the table:



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


When I run the application though I get an error that says:



<class 'KeyError'> 'id'


I have tried other suggestions like adding:



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


to the Field definition but it comes up with the same error.



I've tried breaking the query into two and using the _and option with IS_NOT_IN_DB since it seemed this might give a similar effect:



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


Whilst the above doesn't generate an error it also doesn't exclude the unwanted users in the drop-down correctly either. The only thought I had here was that it might be because the the application is using crud to generate the forms instead of explicitly using SQLFORM and a post I saw mentioned that Crud has essentially been dropped in favour of SQLFORM...



For reference my web2py version is 2.17.2 downloaded from source.



Does anyone know how I might fix the field definition to make this work?



UPDATED (2018/11/200):



Sorry, here's the traceback:



Traceback



    Traceback (most recent call last):
File "C:UsersBenpythonweb2pygluonrestricted.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:UsersBenpythonweb2pygluonglobals.py", line 421, in <lambda>
self._caller = lambda f: f()
File "C:UsersBenpythonweb2pygluontools.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:UsersBenpythonweb2pygluontools.py", line 4374, in create
**attributes
File "C:UsersBenpythonweb2pygluontools.py", line 4296, in update
**attributes # contains hidden
File "C:UsersBenpythonweb2pygluonsqlhtml.py", line 1523, in __init__
inp = self.widgets.options.widget(field, default)
File "C:UsersBenpythonweb2pygluonsqlhtml.py", line 325, in widget
options = requires[0].options()
File "C:UsersBenpythonweb2pygluonvalidators.py", line 2795, in _options
options = self.other.options(*args, **kwargs)
File "C:UsersBenpythonweb2pygluonvalidators.py", line 601, in options
self.build_set()
File "C:UsersBenpythonweb2pygluonvalidators.py", line 594, in build_set
self.theset = [str(r[self.kfield]) for r in records]
File "C:UsersBenpythonweb2pygluonvalidators.py", line 594, in <listcomp>
self.theset = [str(r[self.kfield]) for r in records]
File "C:UsersBenpythonweb2pygluonpackagesdalpydalobjects.py", line 94, in __getitem__
raise KeyError(key)
KeyError: 'id'


Function Argument List



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






web2py






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 3:52







bstredinnick

















asked Nov 16 '18 at 4:20









bstredinnickbstredinnick

112




112













  • Always provide the full traceback.

    – Anthony
    Nov 16 '18 at 13:59











  • Thanks for your comment Anthony. I've updated the question with the traceback and I included the Function Argument List part as well in case it helps.

    – bstredinnick
    Nov 20 '18 at 3:51











  • Actually, using the exact code you provided in the first line above, I cannot reproduce the error. I suggest you post on the Google Group and maybe attach a minimal app that reproduces the problem.

    – Anthony
    Nov 26 '18 at 15:08













  • Ok, thanks for your help Anthony I appreciate it. The app is pretty old so there could be something else there that's breaking it. A simpler reference works without a problem though so for now I'll just add another column to auth_user as a work around and come back to it later.

    – bstredinnick
    Nov 26 '18 at 23:29



















  • Always provide the full traceback.

    – Anthony
    Nov 16 '18 at 13:59











  • Thanks for your comment Anthony. I've updated the question with the traceback and I included the Function Argument List part as well in case it helps.

    – bstredinnick
    Nov 20 '18 at 3:51











  • Actually, using the exact code you provided in the first line above, I cannot reproduce the error. I suggest you post on the Google Group and maybe attach a minimal app that reproduces the problem.

    – Anthony
    Nov 26 '18 at 15:08













  • Ok, thanks for your help Anthony I appreciate it. The app is pretty old so there could be something else there that's breaking it. A simpler reference works without a problem though so for now I'll just add another column to auth_user as a work around and come back to it later.

    – bstredinnick
    Nov 26 '18 at 23:29

















Always provide the full traceback.

– Anthony
Nov 16 '18 at 13:59





Always provide the full traceback.

– Anthony
Nov 16 '18 at 13:59













Thanks for your comment Anthony. I've updated the question with the traceback and I included the Function Argument List part as well in case it helps.

– bstredinnick
Nov 20 '18 at 3:51





Thanks for your comment Anthony. I've updated the question with the traceback and I included the Function Argument List part as well in case it helps.

– bstredinnick
Nov 20 '18 at 3:51













Actually, using the exact code you provided in the first line above, I cannot reproduce the error. I suggest you post on the Google Group and maybe attach a minimal app that reproduces the problem.

– Anthony
Nov 26 '18 at 15:08







Actually, using the exact code you provided in the first line above, I cannot reproduce the error. I suggest you post on the Google Group and maybe attach a minimal app that reproduces the problem.

– Anthony
Nov 26 '18 at 15:08















Ok, thanks for your help Anthony I appreciate it. The app is pretty old so there could be something else there that's breaking it. A simpler reference works without a problem though so for now I'll just add another column to auth_user as a work around and come back to it later.

– bstredinnick
Nov 26 '18 at 23:29





Ok, thanks for your help Anthony I appreciate it. The app is pretty old so there could be something else there that's breaking it. A simpler reference works without a problem though so for now I'll just add another column to auth_user as a work around and come back to it later.

– bstredinnick
Nov 26 '18 at 23:29












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331391%2fhow-can-i-fix-keyerror-when-using-requires-in-web2py-table-field-definition-with%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331391%2fhow-can-i-fix-keyerror-when-using-requires-in-web2py-table-field-definition-with%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$