user1077071
user1077071

Reputation: 961

web2py: getting field from rows object

The 'row' variable returns a single row but it appears the select statement returns a 'rows' object. I want to extract field a and field d from the 'row' variable.

I tried just doing row.a or row.d but this returns an error.

def d_comp():    

    c_id = request.args(0)
    evo_id = request.args(1)

    row = db((db.tbl_stat.c_id == c_id) & (db.tbl_stat.evo_type == evo_id)).select()

    c = db(db.tbl_c.id == c_id).select(db.tbl_c.ALL)

    a = 1

    d = 1

    p = 1

    return dict(c=c,a=a,d=d,p=p)

Upvotes: 0

Views: 3871

Answers (3)

Eddie
Eddie

Reputation: 160

The short answer is put .first() after your .select()

row = db((db.tbl_stat.c_id == c_id) & (db.tbl_stat.evo_type == evo_id)).select().first()

Upvotes: 0

Anthony
Anthony

Reputation: 25536

The .select() method always returns a DAL Rows object, even if it contains no records or just one record. The Rows object acts like a list, so you can extract individual records (which are DAL Row objects) by subscripting:

rows = db((db.tbl_stat.c_id == c_id) & (db.tbl_stat.evo_type == evo_id)).select()
row = rows[0]  # extracts the first record in "rows"

Another convenient method for extracting the first row specifically is:

row = rows.first()

The advantage of that method is that it simply returns None if there are no records in the Rows object rather than raising an exception.

Upvotes: 0

user1077071
user1077071

Reputation: 961

I solved the question by doing the following inside the html file:

{{for val in row:}}
    {{=val.a}}
    {{=val.d}]
{{pass}}

Upvotes: 0

Related Questions