[web2py] Re: field value from row in a view

2015-08-03 Thread Dave S


On Sunday, August 2, 2015 at 9:18:11 AM UTC-7, P Page-McCaw wrote:

 I am having a hard time understanding how to get values from a row in a 
 view. I think I am being really dim. (It would really help us amateurs to 
 have a more full description of how to do database queries. Really I need 
 The Very Stupid Person's Guide to... because I read the guide and then do 
 what it says (or so I think) and can't get it to work.

 The generic problem is how to retrieve data from a linked table in a view. 
 I have problems (this tells you how pathetic I am [really though I'm a good 
 biologist]) both where I want to traverse from the table that carries the 
 'reference other_table' field and from the target table. I keep thinking I 
 get the syntax, but the next time I try, fail.

 I have tried many things and though I can get the entire row to show up on 
 the web page with many variants of:
 {{ =db.table_1_name(m.field_name) }}

 This shows in the page as  Row {'other_table_field_name', 'value'} with 
 the whole row rendered as text. So clearly the row is accessed in the view.

 But what I want is table_1_name.'other_table_field_name' and
 {{ =db.table_1_name(m.field_name).field_name }}
 returns an error: can't get field_name from None Type and m.field_name 
 yields the id for the correct reference to the table_1_name. m is the row 
 of the set being iterated through.

 Somewhere, something is being gently and kindly coerced. But I am not 
 smart enough to figure out what is happening.

 Thanks



While we're waiting for some more details, as Anthony and Jorge requested, 
note that a query typically returns a Rows objects, which is a collection 
of Row objects (essentially a list() of Row objects.  

It just occurred to me that the way you wrote your questions makes it look 
like you're trying to do queries in the view.  It is much more natural to 
do queries in a controller, and my example fits with doing things that way. 
[1]

You'd do something like this to print the second (0 +1) and fifth Row in a 
Rows object:

results = db(table_1_name().select(table_1_name.field_name)
print results[1]
print results[4]


Now we only selected 1 column in this example, but a Row object is 
essentially a Python dictionary, so if we had
 multiple fields,

results = db(table_1_name().select(table_1_name.field1_name, 
table_1_name.field2_name)
print results[1][field1_name]


would use the string field1_name as the key, and print the value of 
field1 in the second Row object returned.

This should look familiar if you've been reading
URL:http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#select

The methods .first() and .last() can pick up the first Row object and 
last Row object without your knowing how many Row objects there are; nice 
convenience tools.

[1] You can also do things in the web2py shell ('python web2py.py -S 
appname', but you need to invoke your model declarations explicitly (I did 
cut-and-paste of the 'db = ' line of db.py, and then the 'db.define _table' 
line of the particular table I was using for checking my answers.

Welcome aboard the web2py cruise; I hope this helps. 

Dave
/dps


-- 
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: field value from row in a view

2015-08-02 Thread Anthony
I think we need to see actual code, including the model definitions and the 
query in the controller, as well as the actual error reported (the NoneType 
error doesn't quite make sense if the previous example did really return a 
Row).

On Sunday, August 2, 2015 at 12:18:11 PM UTC-4, P Page-McCaw wrote:

 I am having a hard time understanding how to get values from a row in a 
 view. I think I am being really dim. (It would really help us amateurs to 
 have a more full description of how to do database queries. Really I need 
 The Very Stupid Person's Guide to... because I read the guide and then do 
 what it says (or so I think) and can't get it to work.

 The generic problem is how to retrieve data from a linked table in a view. 
 I have problems (this tells you how pathetic I am [really though I'm a good 
 biologist]) both where I want to traverse from the table that carries the 
 'reference other_table' field and from the target table. I keep thinking I 
 get the syntax, but the next time I try, fail.

 I have tried many things and though I can get the entire row to show up on 
 the web page with many variants of:
 {{ =db.table_1_name(m.field_name) }}

 This shows in the page as  Row {'other_table_field_name', 'value'} with 
 the whole row rendered as text. So clearly the row is accessed in the view.

 But what I want is table_1_name.'other_table_field_name' and
 {{ =db.table_1_name(m.field_name).field_name }}
 returns an error: can't get field_name from None Type and m.field_name 
 yields the id for the correct reference to the table_1_name. m is the row 
 of the set being iterated through.

 Somewhere, something is being gently and kindly coerced. But I am not 
 smart enough to figure out what is happening.

 Thanks


-- 
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: field value from row in a view

2015-08-02 Thread JorgeH
can you show us your controller? and db model?

On Sunday, August 2, 2015 at 11:18:11 AM UTC-5, P Page-McCaw wrote:

 I am having a hard time understanding how to get values from a row in a 
 view. I think I am being really dim. (It would really help us amateurs to 
 have a more full description of how to do database queries. Really I need 
 The Very Stupid Person's Guide to... because I read the guide and then do 
 what it says (or so I think) and can't get it to work.

 The generic problem is how to retrieve data from a linked table in a view. 
 I have problems (this tells you how pathetic I am [really though I'm a good 
 biologist]) both where I want to traverse from the table that carries the 
 'reference other_table' field and from the target table. I keep thinking I 
 get the syntax, but the next time I try, fail.

 I have tried many things and though I can get the entire row to show up on 
 the web page with many variants of:
 {{ =db.table_1_name(m.field_name) }}

 This shows in the page as  Row {'other_table_field_name', 'value'} with 
 the whole row rendered as text. So clearly the row is accessed in the view.

 But what I want is table_1_name.'other_table_field_name' and
 {{ =db.table_1_name(m.field_name).field_name }}
 returns an error: can't get field_name from None Type and m.field_name 
 yields the id for the correct reference to the table_1_name. m is the row 
 of the set being iterated through.

 Somewhere, something is being gently and kindly coerced. But I am not 
 smart enough to figure out what is happening.

 Thanks


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