Sorry, just occurred to me that your tables are defined in the wrong order 
-- if you want the default validator and represent attribute, you have to 
define the referenced tables (b and c) before the referencing table (bc). 
It can only create the validator and represent attribute if the referenced 
tables actually exist.

As for catching non-existent references, it depends on what you want to 
display in that case. The default represent attribute will result in just 
the stored reference value (i.e., the integer id) being displayed. If 
instead you want something like "N/A" or "missing" to display, then you 
will have to implement your own represent attribute.

Anthony

On Monday, June 24, 2013 3:58:50 AM UTC-4, step wrote:
>
>
>
> On Monday, June 24, 2013 3:32:32 AM UTC+2, Anthony wrote:
>>
>> Another option is to do nothing at all. By default, a reference field 
>> gets a default IS_IN_DB validator, and as long as you don't override that 
>> default validator, it also gets a default represent attribute based on the 
>> format of the referenced field (in fact, that is the exact purpose of the 
>> format attribute).
>
>
> Right, but I don't see it happening here. If I rely on default behavior 
> all I see is ids being printed as integers. I would much prefer to leverage 
> web2py built-in behavior instead of re-inventing the wheel. So now my set 
> of questions expands to include, "Why isn't web2py built-in behavior 
> working in my example?" I must be doing something wrong. I've attached a 
> zip file with a working example, for anyone who would please double-check 
> what keeps web2py from printing referenced formats instead of integer ids.  
> The example controller has two commented-out lines. Uncomment them and 
> reload the page to see the display that I'd expect to see as web2py's 
> built-in behavior. If I understood Anthony correctly.
>  
>
>> Also, if records are entered into db.bc via web2py forms, the default 
>> validator will prevent references to non-existent references.
>>
>> There are also other, non-web2py forms that update this database, so I 
> need to catch non-existent references. 
>
> My set of questions (updated)
> . Are the represent properties in the example a good way to show 
> referenced formats, or is there a better, more web2py-ish way?
> . Which one?
> . What's the fastest way to deal with the unlikely but possible case of 
> ids referencing a non-existent record of b or c?
> . Why isn't web2py built-in behavior (of printing referenced formats) 
> working in my case?
>
> The working example (also attached as a  zip file)
> models/db.py
> # -*- coding: utf-8 -*-
>
> db = DAL('sqlite://storage.sqlite',pool_size=1,check_reserved=['all'])
>
> db.define_table('bc',
>     Field('b_id', 'reference bt', writable=False),
>     Field('c_id', 'reference ct', writable=False))
> db.define_table('bt', Field('x'), Field('y'),
>     format='b drops %(x)s and %(y)s')
> db.define_table('ct', Field('x'), Field('y'),
>     format='c picks %(x)s in %(y)s')
>
>
> controllers/default.py
> # -*- coding: utf-8 -*-
>
> def index():
>
>     if 'bc' in db.tables:
>         db.bt.truncate()
>         db.ct.truncate()
>         db.bc.truncate()
>     b_ids = db.bt.bulk_insert([dict(x='work',y='pay')])
>     c_ids = db.ct.bulk_insert([dict(x='apples',y='May')])
>     db.bc.bulk_insert([dict(b_id=b_ids[0],c_id=c_ids[0])])
>
> #    db.bc.b_id.represent=lambda id,row: db.bt._format % db.bt
> [id].as_dict()
> #    db.bc.c_id.represent=lambda id,row: db.ct._format % 
> db.ct[id].as_dict()
>     grid_bc = SQLFORM.smartgrid(db.bc, linked_tables=['bt','ct'])
>     return dict(grid_bc=grid_bc)
>
>
>
>

-- 

--- 
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to