represent how to send row id in args

56 views
Skip to first unread message

Oliver Holloway

unread,
Mar 6, 2015, 1:37:22 PM3/6/15
to web...@googlegroups.com
In the following table, the session_name is represented as a link. I want to pass session.id in args (rather than args=1 as you see below). How do I do that? I've tried various ways to say args=db.sessions.id, and it's as if the sessions.id value is not being picked up.

db.define_table('sessions',
                Field('program_name', 'string', requires=IS_IN_DB(db, db.programs.program_name)),
                Field('session_type', 'string', requires=IS_IN_SET(['practice', 'evaluation'], zero=None)),
                Field('session_number', 'integer', default=1), # this is the session number by program
                Field('coach', 'string'),
                Field('assistant', 'string'),
                Field('session_date_time', 'datetime'),
                Field('session_name', compute = lambda row: row.program_name + ', ' + row.session_type + ' ' + str(row.session_number))
               )

db.sessions.session_name.represent = lambda session_name, row: A(session_name, _href=URL('tests_for_this_eval', args=1))

Any help is much appreciated.

Anthony

unread,
Mar 6, 2015, 1:47:08 PM3/6/15
to web...@googlegroups.com
It should be args=row.id, not args=db.session.id (the former is the value of the id for the current record, and the latter is the actual id Field object).

Anthony

Oliver Holloway

unread,
Mar 6, 2015, 1:59:08 PM3/6/15
to web...@googlegroups.com
When I try that, this error occurs:  'Row' object has no attribute 'id'

Oliver Holloway

unread,
Mar 6, 2015, 2:02:21 PM3/6/15
to web...@googlegroups.com
This is the controller, in case that matters.

@auth.requires_membership('coach')
def show_list_of_sessions():
  # get list of evaluations
  evaluations = SQLTABLE(db(db.sessions.session_type=='evaluation').select(db.sessions.session_name),
                         headers=None, truncate=128
                        )

  # get list of practices
  practices = SQLTABLE(db(db.sessions.session_type=='practice').select(db.sessions.session_name),
                       headers=None, truncate=128
                      )

  return dict(evaluations=evaluations, practices=practices)


Leonel Câmara

unread,
Mar 6, 2015, 2:02:57 PM3/6/15
to web...@googlegroups.com
you need to write row.sessions.id

You always need to be explicit in represents.

Oliver Holloway

unread,
Mar 6, 2015, 2:13:46 PM3/6/15
to web...@googlegroups.com
Hi, Leonel. When I do that, I get this error:  

'Row' object has no attribute 'sessions'

Anthony

unread,
Mar 6, 2015, 2:16:27 PM3/6/15
to web...@googlegroups.com


On Friday, March 6, 2015 at 2:02:57 PM UTC-5, Leonel Câmara wrote:
you need to write row.sessions.id

You always need to be explicit in represents.

You need to be explicit in virtual fields (i.e., include the table name), but I don't think that is the case with represent. I think the problem here is that in the select, the "id" field is not included, so therefore not available when the represent function is called. Try changing it to:

 .select(db.sessions.id, db.sessions.session_name)

Anthony

Oliver Holloway

unread,
Mar 6, 2015, 2:23:48 PM3/6/15
to web...@googlegroups.com
Added sessions.id to the select, same thing. Here's the table definition and the controller. The id does show up on the view page, but isn't passing as an arg.

db.define_table('sessions',
                Field('program_name', 'string', requires=IS_IN_DB(db, db.programs.program_name)),
                Field('session_type', 'string', requires=IS_IN_SET(['practice', 'evaluation'], zero=None)),
                Field('session_number', 'integer', default=1), # this is the session number by program, using 0 for evaluations
                Field('coach', 'string'),
                Field('assistant', 'string'),
                Field('session_date_time', 'datetime'),
                Field('session_name', compute = lambda row: row.program_name + ', ' + row.session_type + ' ' + str(row.session_number))
               )

db.sessions.session_name.represent = lambda session_name, row: A(row.session_name, _href=URL('demo', 'tests_for_this_eval', args=db.sessions.id))

@auth.requires_membership('coach')
def show_list_of_sessions():
  # get list of evaluations
  evaluations = SQLTABLE(db(db.sessions.session_type=='evaluation').select(db.sessions.id, db.sessions.session_name),
                         headers=None, truncate=128
                        )

  # get list of practices
  practices = SQLTABLE(db(db.sessions.session_type=='practice').select(db.sessions.session_name),
                       headers=None, truncate=128
                      )

  return dict(evaluations=evaluations, practices=practices)

Leonel Câmara

unread,
Mar 6, 2015, 2:30:15 PM3/6/15
to web...@googlegroups.com
You have 'db' instead of 'row' in your args.

Anthony

unread,
Mar 6, 2015, 2:37:37 PM3/6/15
to web...@googlegroups.com
Is that the exact code, because you didn't change to args=row.id, and your second select is still missing the "id" field?

Anthony

Oliver Holloway

unread,
Mar 6, 2015, 2:38:39 PM3/6/15
to web...@googlegroups.com
Sorry, I've been trying various combinations. Here's what I've tried, none of them work. 

#db.sessions.session_name.represent = lambda session_name, row: A(row.session_name, _href=URL('demo', 'tests_for_this_eval', args=sessions.id))
#  global name 'sessions' is not defined

#db.sessions.session_name.represent = lambda session_name, row: A(row.session_name, _href=URL('demo', 'tests_for_this_eval', args=db.sessions.id))
#  page renders, but controller does not pass sessions.id as arg; sessions.id does show up in rendered page as a field

#db.sessions.session_name.represent = lambda session_name, row: A(row.session_name, _href=URL('demo', 'tests_for_this_eval', args=row.id))
#  'Row' object has no attribute 'id'

#db.sessions.session_name.represent = lambda session_name, row: A(row.session_name, _href=URL('demo', 'tests_for_this_eval', args=row.sessions.id))
#  'Row' object has no attribute 'sessions'

Oliver Holloway

unread,
Mar 6, 2015, 2:42:06 PM3/6/15
to web...@googlegroups.com
Hi, Anthony. I've posted the variations I've tried. As for the second select, I should have excised that because it's unimportant right now.

Oliver Holloway

unread,
Mar 6, 2015, 2:50:59 PM3/6/15
to web...@googlegroups.com
I've caused some confusion with my typos. Here's the code.

model
db.define_table('sessions',
                Field('program_name', 'string', requires=IS_IN_DB(db, db.programs.program_name)),
                Field('session_type', 'string', requires=IS_IN_SET(['practice', 'evaluation'], zero=None)),
                Field('session_number', 'integer', default=1), # this is the session number by program, using 0 for evaluations
                Field('coach', 'string'),
                Field('assistant', 'string'),
                Field('session_date_time', 'datetime'),
                Field('session_name', compute = lambda row: row.program_name + ', ' + row.session_type + ' ' + str(row.session_number))
               )

db.sessions.session_name.represent = lambda session_name, row: A(row.session_name, _href=URL('demo', 'tests_for_this_eval', args=1))
#  works, but is hard coded, therefore is not useful in this case
#db.sessions.session_name.represent = lambda session_name, row: A(row.session_name, _href=URL('demo', 'tests_for_this_eval', args=sessions.id))
#  global name 'sessions' is not defined
#db.sessions.session_name.represent = lambda session_name, row: A(row.session_name, _href=URL('demo', 'tests_for_this_eval', args=db.sessions.id))
#  does not pass sessions.id as arg
#db.sessions.session_name.represent = lambda session_name, row: A(row.session_name, _href=URL('demo', 'tests_for_this_eval', args=row.id))
#  'Row' object has no attribute 'id'
#db.sessions.session_name.represent = lambda session_name, row: A(row.session_name, _href=URL('demo', 'tests_for_this_eval', args=row.sessions.id))
#  'Row' object has no attribute 'sessions'

query_sessions = (
  (db.auth_user.id == db.auth_membership.user_id) &
  (db.auth_membership.group_id == db.auth_group.id) &
  (db.auth_group.role.like('coach'))
)

db.sessions.coach.requires = IS_IN_DB(db(query_sessions), db.auth_user.id,  '%(first_name)s %(last_name)s')
db.sessions.assistant.requires = IS_IN_DB(db(query_sessions), db.auth_user.id,  '%(first_name)s %(last_name)s')


controller
@auth.requires_membership('coach')
def show_list_of_sessions():
  # get list of evaluations
  evaluations = SQLTABLE(db(db.sessions.session_type=='evaluation').select(db.sessions.id, db.sessions.session_name),
                         headers=None, truncate=128
                        )

  return dict(evaluations=evaluations)

view
{{extend 'layout.html'}}

<h3>evaluations</h3>
{{=evaluations}}



Oliver Holloway

unread,
Mar 6, 2015, 3:13:56 PM3/6/15
to web...@googlegroups.com
Hey! I switched to vars and it worked :)  Thank you to all of you who helped. 

db.sessions.session_name.represent = lambda session_name, r: A(r.session_name, _href=URL('demo', 'tests_for_this_eval', vars=dict(i=r.id)))


Anthony

unread,
Mar 6, 2015, 3:32:36 PM3/6/15
to web...@googlegroups.com
If vars=dict(i=r.id) works, then so will args=r.id. More specifically, if the latter generates an error saying the Row object has no attribute "id", then the former would also generate the same error, as "r" is the same Row object in both cases. I suspect you made some other change that made it work in the former case.

Anthony

Oliver Holloway

unread,
Mar 6, 2015, 3:48:15 PM3/6/15
to web...@googlegroups.com
Anthony, thanks, I swapped vars back in and that works now. You must be right, I must have made some other change, but I can't see what I did. At any rate, this is working now, and I regret if I've wasted anyone's time.
Reply all
Reply to author
Forward
0 new messages