isinstance(table[field].requires, IS_IN_SET)

Anthony

On Friday, July 26, 2013 8:56:13 AM UTC-4, Sarbjit singh wrote:
>
> Thanks a lot Anthony for  your help. 
>
> I am facing problem while using the variable name with the isinstance 
> method.
>
> i.e. isinstance(db.person.gender.requires, IS_IN_SET) returns TRUE
>
> But If I use it like :
>
> field = db.person.gender
> isinstance(field.requires, IS_IN_SET)   ==> Exception that string has no 
> attribute requires.
>
> Since my code is iterating over all the fields and doing some other 
> operations, I have to store field in variable.
>
> Is there any way I can convert string back to gluon object ?
>
> Thanks
> Sarbjit
>
> On Friday, July 26, 2013 5:30:52 PM UTC+5:30, Anthony wrote:
>>
>> You can do:
>>
>> if hasattr(field.requires, 'options'):
>>
>> which will identify IS_IN_SET and IS_IN_DB validators. If you only want 
>> to identify IS_IN_SET, you can do:
>>
>> if isinstance(field.requires, IS_IN_SET):
>>
>> To get the list of options, you can do field.requires.theset (for the 
>> values), field.requires.labels (for the labels), or 
>> field.requires.options() for a list of tuples of values and labels.
>>
>> You can also create a SELECT object via 
>> SQLFORM.widgets.options.widget(field, 
>> value=field.default).
>>
>> Anthony
>>
>> On Friday, July 26, 2013 4:55:41 AM UTC-4, Sarbjit singh wrote:
>>>
>>> Actually I am not using the SQLFORM. I am using the code for dynamic 
>>> search form from (
>>> http://www.web2pyslices.com/slice/show/1403/dynamic-search). I want to 
>>> modify the code such that for the fields with validators, it should show 
>>> the selection menu. So I want to add  a check in the code below to detect 
>>> if a field is having validator set, then use the SELECT command with the 
>>> validator set values as argument. So far, I am not able to achieve it.
>>>
>>> Can you please recommend something in context here?
>>>
>>> Here is the code from the slice :
>>>
>>> def dynamic_search(table):
>>>     tbl = TABLE()
>>>     selected = []
>>>     ops = ['equals','not equal','greater than','less than',
>>>            'starts with','ends with','contains']
>>>     query = table.id > 0    
>>>     for field in table.fields:
>>>         chkval = request.vars.get('chk'+field,None)
>>>         txtval = request.vars.get('txt'+field,None)
>>>         opval = request.vars.get('op'+field,None)
>>>         row = TR(TD(INPUT(_type="checkbox",_name="chk"+field,
>>>                           value=chkval=='on')),
>>>                  TD(field),TD(SELECT(ops,_name="op"+field,
>>>                                      value=opval)),
>>>                  TD(INPUT(_type="text",_name="txt"+field,
>>>                           _value=txtval)))
>>>         tbl.append(row)
>>>         if chkval:
>>>             if txtval:
>>>                 query &= build_query(table[field], 
>>>                                 opval,txtval)
>>>             selected.append(table[field])           
>>>     form = FORM(tbl,INPUT(_type="submit"))
>>>     results = db(query).select(*selected)
>>>     return form, results
>>>
>>> Thanks,
>>> Sarbjit
>>>
>>>
>>>
>>> On Friday, July 26, 2013 1:37:26 PM UTC+5:30, Massimo Di Pierro wrote:
>>>>
>>>> Exactly. Complete code in case you have errors:
>>>>
>>>> #model
>>>> db.define_table('persons',Field('gender'))
>>>> db.persons.gender.requires = IS_IN_SET(['Male', 'Female'])
>>>>
>>>> #controller default.py
>>>> def index():
>>>>     form = SQLFORM(db.persons).process()
>>>>     return locals()
>>>>
>>>> #views default/index.html
>>>> {{extend 'layout.html'}}
>>>> {{=form}}
>>>>
>>>>
>>>> Mind we tend to call table names with singular not plural (person, not 
>>>> persons). This will make your code more readable.
>>>>
>>>> On Friday, 26 July 2013 02:57:21 UTC-5, viniciusban wrote:
>>>>>
>>>>> It's done automaticaly by web2py. 
>>>>>
>>>>> On Fri, Jul 26, 2013 at 2:29 AM, Sarbjit singh <[email protected]> 
>>>>> wrote: 
>>>>> > I have a db where I have set validator IS_IN_SET on a particular 
>>>>> field. I am 
>>>>> > generating table rows (to be used as form), so I need to check if a 
>>>>> > particular field is having IS_IN_SET validator set and I want to 
>>>>> retrieve 
>>>>> > the set values. Reason I want to do this is that I am generating a 
>>>>> dynamic 
>>>>> > form based on the table fields and for the fields having IS_IN_SET 
>>>>> validtor, 
>>>>> > I want to show the "Drop Down" menu rather than Text Field and wants 
>>>>> to 
>>>>> > populate it with the Validator values. 
>>>>> > 
>>>>> > db.define_table('persons',Field('gender') 
>>>>> > db.persons.gender.requires = IS_IN_SET(['Male', 'Female']) 
>>>>> > 
>>>>> > -Sarbjit 
>>>>> > 
>>>>> > -- 
>>>>> > 
>>>>> > --- 
>>>>> > 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. 
>>>>> > 
>>>>> > 
>>>>>
>>>>

-- 

--- 
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