Ah, it looks like virtual fields is exactly what I'm looking for. That way I can just call
line.speaker.name and have that calculated off of the user's name, etc. Thanks! I knew I had to be missing something obvious to save me the effort. --Greg On Mar 31, 9:26 pm, Anthony <[email protected]> wrote: > I think the format argument only alters the record representation in forms > and SQLTABLE (which is used when a set or records is serialized in a view). > If you're just doing a query and pulling an individual field value from a > row, I don't think the format will be applied. > Seehttp://web2py.com/book/default/chapter/06#Record-Representation. > > If you don't want to call the format function every time you display the > speaker name, maybe you could use a virtual > field:http://web2py.com/book/default/chapter/06#Virtual-Fields > Anthony > > > > > > > > On Thursday, March 31, 2011 4:41:40 PM UTC-4, Gregory Hellings wrote: > > I have defined a custom auth.settings.table_user_name implementation > > that includes one extra field and specifies a value for the format > > argument. In this case format is a callback function which displays > > the user's name with extra formatting based on membership in certain > > auth groups. This option works wonderfully with SQLFORM fields and > > with appadmin displays of the auth_membership table, but I seem to be > > missing something about its functionality elsewhere. > > > (This is in the context of a demo chat application utilizing the > > web2py_comet functionality). I have another table which is defined > > like this: > > > db.define_table('chat', > > Field('speaker', db.auth_user, default=auth.user_id, > > writable=False, readable=False), > > Field('statement', 'text', requires=IS_NOT_EMPTY()), > > Field('deleted', db.auth_user, writable=False, readable=False)) > > db.chat.speaker.requires = IS_IN_DB(db, db.auth_user.id) > > db.chat.deleted.requires = IS_IN_DB(db, db,auth_user.id) > > > My controller for displaying the lines uses this logic > > > rawlines=db(db.chat.deleted==None).select(limitby=(0,10),orderby=~ > > db.chat.id) > > lines=[] > > for line in rawlines: > > lines.append(DIV(line.speaker, B("> "), line.statement, > > _id=line.id)) > > > However, when I do that, I get the user's ID instead of the user's > > name as formatted by my format callback method. Thus the chat output > > looks like > > > 1> Hi there > > 1> How is everyone doing? > > 2> We're great, how about you? > > 1> Oh, I'm absolutely fantastic. > > > In order to get the properly formatted user's name, I have to > > explicitly call my format method, making the last line of the above > > controller instead read > > > lines.append(DIV(name_format(line.speaker), B("> "), > > line.statement, _id=line.id)) > > > This defeats most of the purpose I had in using the automatic > > format=name_format argument to the define_table call. Am I missing > > something here? What am I doing incorrectly? As I scale out of my > > demo chatroom, I want to make sure user names are consistently > > displayed throughout my application by means of the format argument to > > the auth_user table, rather than trying to remember to call > > name_format(user) everywhere. Any advice? > > > --Greg

