It works now. Thanks.
I use the following code to display 'phmap' table.
cols = [ 'phmap.' + c for c in db.phmap.fields()[1:]] # remove the
'id' field and form phmap.F1, phmap.F2,... list
hdrs = dict([(str(c),c.label) for c in db.phmap])
wlist =SQLTABLE(words,
headers = hdrs,
columns=cols,
truncate=32)
wlist['_id'] = 'wlist'
return dict(wlist=wlist)
On Aug 13, 5:07 pm, mdipierro <[email protected]> wrote:
> ./web2py -S myapp -M -N
>
> >>> words= db(db.phmap.id>0).select(limitby=(0,5),orderby='<random>')
>
> -M = import models
> -N = do not run cron
>
> On Aug 13, 3:52 am,dlin<[email protected]> wrote:
>
>
>
> > How can I use shell to try it before modify my web site?
>
> > ./web2py -S myapp will show the >>> prompt of ipython.
> > But, when I type
>
> > words= db(db.phmap.id>0).select(limitby=(0,5),order='<random>')
>
> > it will failed, I guess I must import something.....
>
> > On Aug 13, 4:11 pm, mdipierro <[email protected]> wrote:
>
> > > headers=dict((str(c),c.label) for c in db.phmap)
>
> > > On Aug 13, 3:05 am,dlin<[email protected]> wrote:
>
> > > > tks, Massimo, there is a typo "order" should be "orderby" in your
> > > > sample.
>
> > > > Could I just define the columns, and let headers automatic get from
> > > > predefined labels?
> > > > Because, in fact, I have more fields instead of two.
>
> > > > On Aug 13, 3:23 pm, mdipierro <[email protected]> wrote:
>
> > > > > words= db(db.phmap.id>0).select(limitby=(0,5),order='<random>')
> > > > > table =SQLTABLE(words,
> > > > > headers={'phmap.F1':db.phmap.F1.label,phmap.F2:db.phmap.F2.label'},
> > > > > columns=['phmap.F1',phmap.F2'],
> > > > > truncate=32)
>
> > > > > On Aug 13, 2:02 am,dlin<[email protected]> wrote:
>
> > > > > > I've a table with fields
> > > > > > Field('F1', label='name')
> > > > > > Field('F2', label='data')
> > > > > > ....
> > > > > > Field('F20', label='xxx')
>
> > > > > > I wish use crud to display 5 random rows of the table, and only
> > > > > > display F1, F2.
>
> > > > > > So, here is my code:
>
> > > > > > ids = [x.values()[0] for x in db().select(db.phmap.id).as_list()]
> > > > > > idr = random.sample(ids, 5)
> > > > > > words = db(db.phmap.id.belongs(idr)).select(db.phmap.ALL)
>
> > > > > > I want to display the words with only F1 and F2 fields.
> > > > > > How could I display them by crud.select()?
> > > > > > I want to be smarter to use the already defined labels in db.py,
> > > > > > but
> > > > > > I don't know how to do it.