[web2py] particular IS_IN_DB need advice

60 views
Skip to first unread message

Richard

unread,
Aug 13, 2013, 5:43:12 PM8/13/13
to web...@googlegroups.com
Hello,

I have a situation were I need to use a referenced field from the referenced table...

I have table a that is referenced by table b, so there is a_id FK field in table b. But now I want table c to reference table b but to discriminate record I need the representation of a id in the dropbox of c form because b table are meaning less with a id represent.

It occurs that IS_IN_DB does not let you access the value of the referenced table, I mean I can't do something like this :

IS_IN_DB(db, 'b.id', represent_dict[a_id] + b.other_field)

I thought about using IS_IN_SET and constructing my own key / value there but IS_IN_SET don't allow you to pass a dict instead of list, as far as I know. If I could pass a dict, IS_IN_SET could display the representation of the records of my referenced table but pass the id of this record to form.vars.reference_field instead of the text of the represent.

I must need a custom validator... But I think that a more advanced IS_IN_SET could be welcome in this kind of situation... 

Thanks 

Richard

Anthony

unread,
Aug 13, 2013, 6:03:21 PM8/13/13
to web...@googlegroups.com

I have table a that is referenced by table b, so there is a_id FK field in table b. But now I want table c to reference table b but to discriminate record I need the representation of a id in the dropbox of c form because b table are meaning less with a id represent.

It occurs that IS_IN_DB does not let you access the value of the referenced table, I mean I can't do something like this :

IS_IN_DB(db, 'b.id', represent_dict[a_id] + b.other_field)

The third argument to IS_IN_DB can be a callable that takes the row object from the select, so it can generate whatever representation you want. I'm not sure what you mean by represent_dict[a_id], but perhaps something like:

IS_IN_DB(db, 'b.id', lambda r: db.table_a(r.a_id).some_field + r.other_field)

If the representation is more complex, you can define a separate function rather than using a lambda.

Also, note that IS_IN_SET can take a dictionary or list of tuples -- see the book section on it.

Anthony

Richard Vézina

unread,
Aug 14, 2013, 8:31:51 AM8/14/13
to web2py-users
Great!!

Thank you Anthony for clarification. That exactly what I was needing.

I should have read more carefully validators section I guess.

:)

Richard


--
 
---
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+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Richard Vézina

unread,
Aug 14, 2013, 8:42:48 AM8/14/13
to web2py-users
About represent_dict[...], I use to construct dict of id (key) / represent (value) for some table where the represent if formed from the concatation of attribute from differents tables. So instead of having to query 2 tables I just call my dict... Of course, I have to update once in a wild the dict, but since that these values are read more often than they update, it is not a big deal.

Richard


Reply all
Reply to author
Forward
0 new messages