[web2py] Re: fields strings to variable for fields=fields

2015-03-28 Thread Anthony
On Friday, March 27, 2015 at 3:39:38 PM UTC-4, LoveWeb2py wrote:

 Hi JorgeH,

 I didn't want to use eval due to code injection, but here is what I ended 
 up using and it worked.

 field_list = ['db.mytable.field1', 'db.mytable.field2'] 

 fields = [getattr(db.mytable, i.split(.)[-1] for i in field_list]


Note, you don't need to use getattr() -- just do:

[db.mytable[i.split('.')[-1]] for i in field_list]

A better option might be to change the function that returns the list so it 
returns just the field names (why does it need to return fields in 
'db.mytable.field1' format?).

Anthony

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


[web2py] Re: fields strings to variable for fields=fields

2015-03-28 Thread Gray Kanarek
With the information here 
http://www.web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Run-time-field-and-table-modification

you can do db['mytable']['field1']. So you'd want something like: 

field_list_split = [i.split(.) for i in field_list]
fields = [db[table][field] for dummy, table, field in field_list_split]


On Friday, March 27, 2015 at 3:39:38 PM UTC-4, LoveWeb2py wrote:

 Hi JorgeH,

 I didn't want to use eval due to code injection, but here is what I ended 
 up using and it worked.

 field_list = ['db.mytable.field1', 'db.mytable.field2'] 

 fields = [getattr(db.mytable, i.split(.)[-1] for i in field_list]



 and in SQLFORM fields=fields

 Thanks everyone!1

 On Friday, March 27, 2015 at 3:35:57 PM UTC-4, JorgeH wrote:


 have you tried  eval  ??

 fields = [eval ('db.mytable.field1') , eval ('db.mytable.field2') ] 





 (just a suggestion. I didn't tried myself)

 On Friday, March 27, 2015 at 2:24:06 PM UTC-5, LoveWeb2py wrote:

 having trouble with the SQLFORM.smartgrid fields section

 Fields only takes 

 fields = [db.mytable.field1, db.mytable.field2]

 I have a function that returns all the fields to be returned, but 
 they're in strings format.

 fields = ['db.mytable.field1', 'db.mytable.field2'] #this returns an 
 error with SQLFORM

 How can I convert the strings to variables like in the first fields 
 example.



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


[web2py] Re: fields strings to variable for fields=fields

2015-03-27 Thread JorgeH

have you tried  eval  ??

fields = [eval ('db.mytable.field1') , eval ('db.mytable.field2') ] 



(just a suggestion. I didn't tried myself)

On Friday, March 27, 2015 at 2:24:06 PM UTC-5, LoveWeb2py wrote:

 having trouble with the SQLFORM.smartgrid fields section

 Fields only takes 

 fields = [db.mytable.field1, db.mytable.field2]

 I have a function that returns all the fields to be returned, but they're 
 in strings format.

 fields = ['db.mytable.field1', 'db.mytable.field2'] #this returns an error 
 with SQLFORM

 How can I convert the strings to variables like in the first fields 
 example.


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


[web2py] Re: fields strings to variable for fields=fields

2015-03-27 Thread LoveWeb2py
Hi JorgeH,

I didn't want to use eval due to code injection, but here is what I ended 
up using and it worked.

field_list = ['db.mytable.field1', 'db.mytable.field2'] 

fields = [getattr(db.mytable, i.split(.)[-1] for i in field_list]



and in SQLFORM fields=fields

Thanks everyone!1

On Friday, March 27, 2015 at 3:35:57 PM UTC-4, JorgeH wrote:


 have you tried  eval  ??

 fields = [eval ('db.mytable.field1') , eval ('db.mytable.field2') ] 





 (just a suggestion. I didn't tried myself)

 On Friday, March 27, 2015 at 2:24:06 PM UTC-5, LoveWeb2py wrote:

 having trouble with the SQLFORM.smartgrid fields section

 Fields only takes 

 fields = [db.mytable.field1, db.mytable.field2]

 I have a function that returns all the fields to be returned, but they're 
 in strings format.

 fields = ['db.mytable.field1', 'db.mytable.field2'] #this returns an 
 error with SQLFORM

 How can I convert the strings to variables like in the first fields 
 example.



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