[web2py] Re: query and fields involved

2014-12-11 Thread Anthony
Maybe something like this:

def query_fields(query):
from gluon.dal import Query
fields = []
for element in (query.first, query.second):
if isinstance(element, Field):
fields.append(element)
elif isinstance(element, Query):
fields.extend(query_fields(element))
return fields

The above will return a list of field objects. You could instead have it 
return a list of field names (either using field.name, which returns only 
the field name, or using str(field), which returns 'tablename.fieldname' 
format), or directly check for the existence of a particular field.

Note, when checking for a field match, don't do something like:

db.mytable.myfield in list_of_field_objects

or:

any(db.mytable.myfield == field for field in list_of_field_objects)

In both cases, the comparison operator will result in a Query object 
(because of the ==), which will always yield True. Instead, use "is" or 
compare the field names/string representations:

any(db.mytable.myfield is field for field in list_of_field_objects)

or:

any(str(db.mytable.myfield) == str(field) for field in list_of_field_objects
)

Anthony

On Thursday, December 11, 2014 9:51:44 AM UTC-5, Massimiliano wrote:
>
> Is there a way to know if a field is involved in a query inspecting the 
> query object?
>
> Thank you
> -- 
> Massimiliano
>  

-- 
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/d/optout.


Re: [web2py] Re: query and fields involved

2014-12-12 Thread Massimiliano
Thank you very much Antony!


On Thu, Dec 11, 2014 at 5:01 PM, Anthony  wrote:

> Maybe something like this:
>
> def query_fields(query):
> from gluon.dal import Query
> fields = []
> for element in (query.first, query.second):
> if isinstance(element, Field):
> fields.append(element)
> elif isinstance(element, Query):
> fields.extend(query_fields(element))
> return fields
>
> The above will return a list of field objects. You could instead have it
> return a list of field names (either using field.name, which returns only
> the field name, or using str(field), which returns 'tablename.fieldname'
> format), or directly check for the existence of a particular field.
>
> Note, when checking for a field match, don't do something like:
>
> db.mytable.myfield in list_of_field_objects
>
> or:
>
> any(db.mytable.myfield == field for field in list_of_field_objects)
>
> In both cases, the comparison operator will result in a Query object
> (because of the ==), which will always yield True. Instead, use "is" or
> compare the field names/string representations:
>
> any(db.mytable.myfield is field for field in list_of_field_objects)
>
> or:
>
> any(str(db.mytable.myfield) == str(field) for field in
> list_of_field_objects)
>
> Anthony
>
>
> On Thursday, December 11, 2014 9:51:44 AM UTC-5, Massimiliano wrote:
>>
>> Is there a way to know if a field is involved in a query inspecting the
>> query object?
>>
>> Thank you
>> --
>> Massimiliano
>>
>  --
> 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/d/optout.
>



-- 
Massimiliano

-- 
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/d/optout.