How does db.table.field.represent work in the view?

24 views
Skip to first unread message

__future__

unread,
Aug 6, 2009, 8:29:06 PM8/6/09
to web2py-users
I am sure this is something simple that I am missing here when you do
something like:

#model
db.define_table('event',
Field('description','text'),
Field('creation_date','datetime', default=request.now),
Field('start_date','datetime'),
Field('creator', db.auth_user,default=auth.user.id if
auth.is_logged_in() else 0),
Field('latest','boolean',default=True))

db.event.creator.represent=lambda id: db.auth_user[id].email

#controller
def tonight():

from datetime import datetime, timedelta

now = datetime.now()
start = datetime.min.replace(year=now.year, month=now.month,
day=now.day)
end = (start + timedelta(days=1)) - timedelta.resolution

tonight_query = (db.event.start_date > start) &
(db.event.start_date < end) & (db.event.latest == True)
events = db(tonight_query).select()
return dict(events=events)

#view
{{extend 'layout.html'}}
<h1>Events Tonight</h1>
<ul>
{{ for event in events: }}
</li>{{ =event.creator }} : {{ =event.description }}</li>
{{ pass }}
</ul>

The {{ =event.creator }} is still always an integer rather than using
the representation defined in the model (db.auth_user.email).

If I add {{=BEAUTIFY(response._vars)}} to the view, the table shows
the event.creator field with the correct db.auth_user.email
representation

What am I missing here?

DenesL

unread,
Aug 7, 2009, 12:16:49 AM8/7/09
to web2py-users
event.creator in the view is not a field, it is just an element in a
row
besides represent is only used by SQLFORM and SQLTABLE (the later
explains why BEAUTIFY works).

If you want to use represent in the view you would have to do so
explicitly:

{{ for e in events: }}
        <li>{{ =db.event.creator.represent(e.creator) }} :
{{ =e.description }}</li>
{{ pass }}
Reply all
Reply to author
Forward
0 new messages