db = DAL()
db.define_table('person',Field('name'))
db.define_table('thing',Field('name'),Field('owner','reference person'))
forname in('Max','Tim','Jim'):
i = db.person.insert(name=name)
forthing in('Chair','Table','Bike'):
db.thing.insert(owner=i, name=name+"'s "+thing)
rows = db(db.thing).select().join(db.person.id)
print(rows.as_json())
"""
[{"owner": {"id": 1, "name": "Max"}, "id": 1, "name": "Max's Chair"},
{"owner": {"id": 1, "name": "Max"}, "id": 2, "name": "Max's Table"},
{"owner": {"id": 1, "name": "Max"}, "id": 3, "name": "Max's Bike"},
{"owner": {"id": 2, "name": "Tim"}, "id": 4, "name": "Tim's Chair"},
{"owner": {"id": 2, "name": "Tim"}, "id": 5, "name": "Tim's Table"},
{"owner": {"id": 2, "name": "Tim"}, "id": 6, "name": "Tim's Bike"},
{"owner": {"id": 3, "name": "Jim"}, "id": 7, "name": "Jim's Chair"},
{"owner": {"id": 3, "name": "Jim"}, "id": 8, "name": "Jim's Table"},
{"owner": {"id": 3, "name": "Jim"}, "id": 9, "name": "Jim's Bike"}]
"""
rows = db(db.person).select().join(db.thing.owner, name="owns",
fields=[db.thing.id, db.thing.name])
print(rows.as_json())
"""
[{"id": 1, "name": "Max", "owns": [
{"id": 1, "name": "Max's Chair"},
{"id": 2, "name": "Max's Table"},
{"id": 3, "name": "Max's Bike"}]},
{"id": 2, "name": "Tim", "owns": [
{"id": 4, "name": "Tim's Chair"},
{"id": 5, "name": "Tim's Table"},
{"id": 6, "name": "Tim's Bike"}]},
{"id": 3, "name": "Jim", "owns": [
{"id": 7, "name": "Jim's Chair"},
{"id": 8, "name": "Jim's Table"},
{"id": 9, "name": "Jim's Bike"}]}
]
"""
this is designed to be efficient and work on GAE too as long as rows
is not too long.
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google
Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to web2py+unsubscr...@googlegroups.com
<mailto:web2py+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.