Skip to content

Instantly share code, notes, and snippets.

@jotbe
Created September 30, 2011 23:01
Show Gist options
  • Save jotbe/1255259 to your computer and use it in GitHub Desktop.
Save jotbe/1255259 to your computer and use it in GitHub Desktop.
web2py: DAL dict with specific fields including id
>>> # Will only return the two requested fields
>>> db(db.page.name=='delft').select(db.page.name, db.page.title).first()
<Row {'name': 'delft', 'title': 'Delft'}>
>>> # Adding the primary key 'id' will return the requested fields BUT all objects and functions too
>>> db(db.page.name=='delft').select(db.page.id, db.page.name, db.page.title).first()
<Row {'comment': <gluon.dal.Set object at 0x102ca8d50>, 'name': 'delft', 'title': 'Delft', 'update_record': <function <lambda> at 0x102cbc488>, 'page_archive': <gluon.dal.Set object at 0x102ca8c90>, 'document': <gluon.dal.Set object at 0x102cba4d0>, 'id': 3, 'delete_record': <function <lambda> at 0x102cbc578>}>
>>> # Using as_dict() will only return the requested fields
>>> db(db.page.name=='delft').select(db.page.id, db.page.name, db.page.title).first().as_dict()
{'name': 'delft', 'title': 'Delft', 'id': 3}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment