Massimo, that's great!!

Could work with multiple levels of relations? Giving something like:

{"owner": {"id": 1, "name": "Max", "owner_of_owner":{"id": 1, "data": "the data"}}, "id": 1, "name": "Max's Chair"}

Or can be used to build that (https://groups.google.com/d/topic/web2py/89byxw7BYwc/discussion) functionality? (Maybe I can build it now using your code as base, with other name as Anthony suggests)

Greetings.

El 05/07/16 a las 14:20, Massimo Di Pierro escribió:

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.



--
Este mensaje le ha llegado mediante el servicio de correo electronico que 
ofrece Infomed para respaldar el cumplimiento de las misiones del Sistema 
Nacional de Salud. La persona que envia este correo asume el compromiso de usar 
el servicio a tales fines y cumplir con las regulaciones establecidas

Infomed: http://www.sld.cu/

--
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to