Simple example:
db=DAL('sqlite://test')
db.define_table('purchase',
Field('item'),
Field('unit_price','double'),
Field('quantity','integer'))
db.purchase.insert(item='Box',unit_price=15,quantity=3)
rows=db().select(db.purchase.ALL)
class purchase_meta:
_tablename='purchase'
def __init__(self,tax):
self.tax=tax
def revenues(self):
return
self.purchase.unit_price*self.purchase.quantity*self.tax
rows.meta=purchase_meta(1.07)
for row in rows:
print row.item,
row.unit_price,'*',row.quantity,'*',row.tax,'=',row.revenues
More complex example:
db.define_table('a',Field('n','integer'))
db.define_table('b',Field('n','integer'),Field('a',db.a))
id = db.a.insert(n=4)
for i in range(3,5): db.b.insert(n=i,a=id)
rows=db(db.b.a==db.a.id).select() ### join
class products:
_tablename='c'
def n(self): return self.a.n*self.b.n
rows.meta=products()
for row in rows:
print row.a.n,'*',row.b.n,'=' row.c.n
Any suggestions on improving the syntax? Django can do the same but
only at the level of the individual table. web2py can now to it for
joins!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---