on_define example to improve the book: please help.

56 views
Skip to first unread message

Tim Richardson

unread,
Jul 22, 2014, 10:01:33 PM7/22/14
to web...@googlegroups.com
I'm trying to improve ch 6 in the book, DAL. 
The on_define parameter to define_table is not documented. 

This is my current text, but the example concerns me because it seems redundant to me. That is, until I found the example, I thought that adding such simple requires settings via Field definitions would still leave the table lazily defined.  

on_define

on_define is a callback triggered when a lazy_table is instantiated, although it is called anyway if the table is not lazy. This allows dynamic changes to the table without losing the advantages of delayed instantiation.

Example:

 db = DAL(lazy_tables=True) 
 db.define_table('person',Field('name'),Field('age','integer'), 
    on_define=lambda table: [ 
            table.name.set_attributes(requires=IS_NOT_EMPTY(),default=''), 
            table.age.set_attributes(requires=IS_INT_IN_RANGE(0,120),default=30), 



Anthony

unread,
Jul 22, 2014, 11:20:16 PM7/22/14
to web...@googlegroups.com
I think you're right -- those attributes shouldn't trigger the lazy definition. on_define would be more useful in case something expensive is happening in defining one of the attributes. Also, if you define an IS_IN_DB or IS_NOT_IN_DB that has a Set object as the first argument (as the query will involve doing something like db.sometable.somefield == some_value, which would cause sometable to be defined).

Anthony

Tim Richardson

unread,
Jul 22, 2014, 11:31:20 PM7/22/14
to web...@googlegroups.com

On Wed, Jul 23, 2014 at 1:20 PM, Anthony <abas...@gmail.com> wrote:
I think you're right -- those attributes shouldn't trigger the lazy definition. on_define would be more useful in case something expensive is happening in defining one of the attributes. Also, if you define an IS_IN_DB or IS_NOT_IN_DB that has a Set object as the first argument (as the query will involve doing something like db.sometable.somefield == some_value, which would cause sometable to be defined).

thanks, I will amend my PR..


--



Tim Richardson, Director
GrowthPath, Data-driven profit growth

Mobile: +61 423 091 732
Office: +61 3 8678 1850
I tweet useful business & IT tips at growthpath_au

GrowthPath Pty Ltd
ABN 76 133 733 963





黄祥

unread,
Jul 23, 2014, 12:37:03 AM7/23/14
to web...@googlegroups.com
 db = DAL(lazy_tables=True) 

not sure, if lazy_tables = True is mandatory or not, another example for on_define :
e.g.
def on_define_bank(table):
    # default
    table.name.default = ''
    table.age.default = 30
    # label
    table.name.label = T('Name')
    table.age.label = T('Age')
    # notnull
    table.name.notnull = True
    table.age.notnull = True
    # required
    table.name.required = True
    table.age.required = True
    # requires
    table.name.requires = IS_NOT_EMPTY()
    table.age.requires = IS_INT_IN_RANGE(0, 120)

db.define_table('person',
    Field('name'),
    Field('age', 'integer'),
    on_define = on_define_bank)

best regards,
stifan

Tim Richardson

unread,
Jul 23, 2014, 12:57:18 AM7/23/14
to web...@googlegroups.com
I believe that on_define is always done at table instantiation, so for non lazy tables it's done when the request is executed as part of setting up the table. so lazy_tables is not mandatory.


--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/t5XrUYK3WDg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Niphlod

unread,
Jul 23, 2014, 6:37:54 AM7/23/14
to web...@googlegroups.com
on_define was specifically created to keep lazy tables lazy. If lazy tables did not exist, on_define would have been useless.

Historically, large pieces of examples relied on something like
db.define_table('b', Field('c'))

db
.b.c.default = 'me'
db
.b.c.requires = IS_IN_SET(('me', 'you'))

I guess it was for cleanliness in reading.... I never liked it. 
That being said, lots of web2py users started copy/pasting ..... and then found out that, with lazy_tables enabled, basically this method of coding meant that you couldn't have "b" lazy because calling db.b.c effectively istantiate the table (even if you don't use it later in the controllers)

ATM, what I'm using on_define for is only database callbacks.

Tim Richardson

unread,
Jul 23, 2014, 7:01:05 AM7/23/14
to web...@googlegroups.com

On Wed, Jul 23, 2014 at 8:37 PM, Niphlod <nip...@gmail.com> wrote:
ATM, what I'm using on_define for is only database callbacks.

I didn't know it existed until I looked at the code :) Thanks.

 rname is finally documented in my PR, as well.

Niphlod

unread,
Jul 23, 2014, 3:46:38 PM7/23/14
to web...@googlegroups.com
thank you as well for your efforts in improving documentation....
Reply all
Reply to author
Forward
0 new messages