db.port.hba.requires = IS_IN_DB(db, 'hba.name')
db.port.switch.requires = IS_IN_DB(db, 'switch.name')

should be

db.port.hba.requires = IS_IN_DB(db, 'hba.id' '%(name)s')
db.port.switch.requires = IS_IN_DB(db, 'switch.id','%(name)s')

Let us know if it fixes the problem. There may be something else.

On Jun 23, 6:20 am, samwyse <samw...@gmail.com> wrote:
> I have a database with three tables, switch, hba and port.  The latter
> is defined as:
> db.define_table('port',
>     SQLField('switch', db.switch),
>     SQLField('name', 'string'),
>     SQLField('hba', db.hba),
>     )
> db.port.name.requires = IS_NOT_EMPTY()
> db.port.hba.requires = IS_IN_DB(db, 'hba.name')
> db.port.switch.requires = IS_IN_DB(db, 'switch.name')
>
> Perhaps foolishly, I want to use the same page to display all of the
> ports for a given switch or for a given hba, so I added 'subset' and
> 'subset_id' to my request:
>  http://127.0.0.1:8000/env_layout/default/ports?subset=switch&subset_id=1
>
> Here's the code from my controller:
>
> def ports():
>     subset, subset_id = request.vars.subset, request.vars.subset_id
>     if request.vars.subset:
>         ports = db().select(db.port[subset] == subset_id,
> orderby=db.port.name)
>     else:
>         ports = db().select(db.port.ALL, orderby=db.port.name)
>     return dict(records=ports, subset=subset, subset_id=subset_id)
>
> And here's my ports.html:
> {{extend "layout.html"}}
> <h1>Select a port...</h1>
> <table>
> <tr>
> {{for field in ['name', 'switch', 'hba']:}}
>   {{if field != subset:}}
>     <th>{{=field}}</th>
>     {{pass}}
>   {{pass}}
> </tr>
> {{for port in records:}}
>   <tr>
>   {{for field in ['name', 'switch', 'hba']:}}
>     {{if field == "name":}}
>       <td>{{=A(port[field], _href=URL(r=request, f="port?id=%s" %
> port.id))}}</td>
>     {{elif field != subset:}}
>       <td>{{=port[field]}}</td>
>     {{pass}}
>   {{pass}}
>   </tr>
> {{pass}}
> </table><br><br>
> {{=A("Add a new port", _href=URL(r=request, f="new_port"))}}
>
> This query works OK:  http://127.0.0.1:8000/env_layout/default/ports
> But this one generates a error:    
> http://127.0.0.1:8000/env_layout/default/ports?subset=switch&subset_id=1
>
> Traceback (most recent call last):
>   File "gluon/restricted.py", line 107, in restricted
>   File "C:\Program Files\web2py\applications\env_layout/views/default/
> ports.html", line 40, in <module>
>   File "gluon/sql.py", line 2363, in __iter__
>   File "gluon/sql.py", line 2271, in __getitem__
>   File "gluon/sql.py", line 1192, in __getitem__
> KeyError: 'switch=1'
>
> response.write('</div>\r\n        \r\n<h1>Select a port...</h1>\r
> \n<table>\r\n<tr>\r\n',escape=False)
> for field in ['name', 'hba', 'switch']:
>     response.write('\r\n',escape=False)
>     if field != subset:
>         response.write('\r\n<th>',escape=False)
>         response.write(field)
>         response.write('</th>\r\n',escape=False)
>         pass
>     response.write('\r\n',escape=False)
>     pass
> response.write('\r\n</tr>\r\n',escape=False)
> for port in records:   ### this is line 40 ###
>     response.write('\r\n<tr>\r\n',escape=False)
>     for field in ['name', 'hba', 'switch']:
>         response.write('\r\n',escape=False)
>         if field == "name":
>             response.write('\r\n<td>',escape=False)
>             response.write(A(port[field], _href=URL(r=request, f="port?
> id=%s" % port.id)))
>             response.write('</td>\r\n',escape=False)
>         elif field != subset:
>             response.write('\r\n<td>',escape=False)
>             response.write(port[field])
>             response.write('</td>\r\n',escape=False)
>             pass
>         response.write('\r\n',escape=False)
>         pass
>     response.write('\r\n</tr>\r\n',escape=False)
>     pass
> response.write('\r\n</table><br><br>\r\n',escape=False)
>
> Any idea what's going on?  Or am I doing this all wrong and should be
> taking a different approach?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to