Interesting question, I have a similar problem. Really, why can't we select 
only one field with select() ?

For example, If i have several field in table Detailtip2 and want to select 
only top 10 amounts:
rows = db((db.Detailtip2.year==session.year) & 
(db.Detailtip2.budget==session.budget)).select(db.Detailtip2.amount, 
orderby=~db.Detailtip2.amount, limitby=(0, 9))

The problem is that rows gets populated with all fields from table.Usually, 
it's not a problem, but in this case i want to populate a list for bar 
chart:
chart_data_list = rows.as_list()

It doesn't work as expected since I got all fields and can't draw a chart. 
OK, I have another solution for this:
    for row in rows:
        chart_data_list.append(row.amount/)

But I still have a question: How can I select one specific field?

Thanks - Kenan

On Saturday, March 9, 2013 5:38:57 PM UTC+1, jjg0 wrote:
>
> Hello again, I have another problem I've run into maybe someone can help 
> with.
>
> Let's say I have a table of blog posts and all I want to do is print out 
> all the titles.  I'll make up a really simple example:
>
> MODEL:
>
> db.define_table('myBlog',
> Field('title'),
> Field('body', 'text'),
> Field('created_on', 'datetime', default=request.now))
>
> CONTROLLER:
>
> def index():
> titles = db(db.myBlog).select()
> return locals()
>
> INDEX VIEW:
>
> {{for title in titles:}}
> {{=title.title}}
> {{pass}}
>
> Now this works, but isn't it a waste to grab all the body and created_on 
> fields if I don't plan on using them?  
> I'm looking at the web2py book and it says in the select section of the 
> DAL:
>
> The select command can take arguments. All unnamed arguments are 
> interpreted as the names of the fields that you want to fetch. For example, 
> *you can be explicit on fetching field "id" and field "name"*:
>
> >>> for row in db().select(db.person.id, db.person.name):
>         print row.name
> Alex
> Bob
> Carl
>
>
> So if I am understanding this correctly, I should be able to edit the 
> controller above to say:
>
>
> def index():
> titles = db().select(db.myBlog.title)
> return locals()
>
> and titles should now only contain the title fields of myBlog.  So if I 
> add something crazy in the view like this:
>
> VIEW:
>
> {{for title in titles:}}
> {{=title.title}}
> *{{=title.body}}*
> {{pass}}
>
> I am expecting this to break, but this works fine.  Shouldn't the 
> {{=title.body}} part break the view now that I am only grabbing the 
> titles?  Shouldn't title.body not exist?  
> Why am I still able to print out the title.body, title.created_on date, 
> and title.id?  
> Does fetching only the title save me anything in the long run, or will 
> grabbing the entire table cost the same as far as performance?  
> What have I misinterpreted with the example taken from the book?
>
> Thanks
>
>

-- 

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