To get the actual ID field of the referenced table, you can also do:

db.mytable.myfield.referent

and to get the table:

db.mytable.myfield.referent.table

and to get the table name:

db.mytable.myfield.referent.tablename

Anthony

On Wednesday, February 12, 2014 5:40:00 PM UTC-5, Anthony wrote:
>
> A reference field has type "reference tablename", so you can do:
>
> db.mytable.myfield.type.split(' ')[-1]
>
> Anthony
>
> On Wednesday, February 12, 2014 3:58:58 PM UTC-5, Ian W. Scott wrote:
>>
>> I've been working for a while on a very flexible (and so increasingly 
>> complex) multiple-select widget that can be re-populated from the db via 
>> ajax (without submitting the form). One think I had to do was figure out 
>> how to get the name of the table referenced by the field using my widget. 
>> With some help from the folks here I found this solution:
>>
>>     referencetable = myfield.requires[0].ktable
>>
>> I'm starting to realize, though, that this is fragile. I know it's doing 
>> an end-run around the DAL api (accessing implementation details instead of 
>> the stable api). I also found recently that this was broken by the 
>> IS_EMPTY_OR validator. For the moment I'm hacking a  solution by doing this:
>>
>>     try:
>>         referencetable = myfield.requires[0].ktable
>>     except AttributeError:  
>>         # because IS_EMPTY_OR doesn't have a property .ktable, 
>>         # it's on the second, wrapped validator
>>         referencetable = myfield.requires[0].other.ktable
>>
>> But I'm digging myself a deeper hole. Now I'm using a second 
>> implementation detail (the "other" instance variable of IS_EMPTY_OR) and my 
>> widget is that much more prone to being broken by future releases.
>>
>> So I'm wondering whether 
>>
>> (a) there's a way to access this data via the api that I'm just missing; 
>> or 
>> (b) this is reason to add access to the referenced table to the api.
>>
>> This could be as simple as adding a get_reference_table() method to the 
>> validators that (for the time being) draws on the instance variables I've 
>> been using. 
>>
>>     # in IS_EMPTY_OR
>>     def get_reference_table(self):
>>         return self.other.ktable
>>
>>     # in IS_IN_DB
>>     def get_reference_table(self):
>>         return self.ktable
>>
>> I'm not sure, though, whether the validator is the best place for that 
>> method. Should it be a method of Field instead? (That is, if others see a 
>> need for this at all.)
>>
>> Cheers,
>>
>> Ian
>>
>

-- 
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 the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to