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). Also, if records are entered into db.bc via web2py
forms, the default validator will prevent references to non-existent
references.
Anthony
On Sunday, June 23, 2013 6:56:33 PM UTC-4, step wrote:
>
> Given three tables, bc, b and c:
>
> define_table('bc', Field('b'), Field('c'),
> Field('b_id', 'reference b', writable=False),
> Field('c_id', 'reference c', writable=False))
> define_table('b', Field('x'), Field('y'),
> format='b drops %(x)s and %(y)s')
> define_table('c', Field('x'), Field('y'),
> format='c picks %(x)s in %(y)s')
>
> I want to show bc's records through the formats of b and c, i.e.,
> "b drops work and pay", "c picks apples in may"
>
> So I came up with this code for bc's controller:
> db.bc.b_id.represent=lambda id,row: db.b._format % db.b[id].as_dict()
> db.bc.c_id.represent=lambda id,row: db.c._format % db.c[id].as_dict()
> SQLFORM.smartgrid(db.bc, linked_tables['b','c'])
>
>
> Questions:
> . Are those represent properties a good way to do it, 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 id
> referencing a non-existent record of b or c? My plan would be something
> like:
>
> db.bc.b_id.represent=lambda id,row: safe_b(id, row)
> def safe_b(id ,row):
> try:
> return db.b._format % db.b[id].as_dict()
> return 'found broken link!'
>
> For what begins to look like a lot of repeated code to simply show b's and
> c's formats in bc.
>
> Thanks in advance
>
>
--
---
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.