Here is the problem. Perhaps you have a solution. format is used to represent a record in a reference as a select/ option. Probably you have many of these records and you only want to fetch columns that are necessary to build the representation.
By using format='%(first_name)s %(last_name)s' web can parse it and easily figure out that you only need first_name and last_name. My example above is convoluted but it allows you to create an arbitrary representation and specify exactly which fields you need. By using represent you are passing a (lambda) function. How would web2py determine which columns to fetch? Should it fetch them all? It could be slow. Massimo On Jan 8, 5:13 am, "KONTRA, Gergely" <pihent...@gmail.com> wrote: > Since the patch is about 2 lines of code, does it make sense to have a > represent for tables, similar for fields, so that I could write > something like: > > db.post.represent = lambda row: row['title'], which means the same as > using format='%(title)s' on define_table? > thx > Gergo > > On Thu, Jan 7, 2010 at 20:32, mdipierro <mdipie...@cs.depaul.edu> wrote: > > Now I understand. > > > Short answer. It cannot be done in the sense there is no API for it > > > Long answer. It can be done this way: > > > ### define table post and set the format > > ### based on a field that does not exist > > db.define_table('post', > > Field('title',length=256), > > Field('body','text',requires=IS_NOT_EMPTY()), > > Field('author',db.auth_user), > > format='%(title_short)s', > > ) > > > ### define the field as a virtual field computed > > class ComputedFieldsForPost: > > def title_short(self): > > return self.post.title[:40]+'...' > > db.post.virtualfields.append(ComputedFieldsForPost()) > > > ### define the referencing table > > db.define_table('comment', > > Field('post',db.post,writable=False,readable=False), > > Field('author',db.auth_user,writable=False,readable=False), > > Field('body','text',requires=IS_NOT_EMPTY())) > > > ### modify an internal of the validator to > > ### fetch data required to compute the virtual field > > db.comment.post.requires.fields=['post.id','post.title'] > > > This kind of ugly but powerful. > > > On Jan 7, 12:25 pm, "KONTRA, Gergely" <pihent...@gmail.com> wrote: > >> Sure. > > >> db.py: > > >> db.define_table('post', > >> Field('title',length=256), > >> Field('body','text',requires=IS_NOT_EMPTY()), > >> Field('author',db.auth_user), > >> ) > > >> db.define_table('comment', > >> Field('post',db.post,writable=False,readable=False), > >> Field('author',db.auth_user,writable=False,readable=False), > >> Field('body','text',requires=IS_NOT_EMPTY())) > > >> In the example I provided, in the appadmin of comment, auth_user is > >> displayed as a dropdown. I would like to do that for comment.post > >> also. > > >> The format option make it possible to hack up some fields of the > >> database, but it is not so flexible to display for eg. computed values > >> of the post table. > > >> thanks in advance > >> Gergo > >> On Thu, Jan 7, 2010 at 16:35, mdipierro <mdipie...@cs.depaul.edu> wrote: > >> > Hi Gergo, > > >> > appadmin has nothing to do with t2 and t2 was deprecated long ago. > > >> > I guess you can still define db.post.represent but I am not sure what > >> > do you want to do with it. What behavior do you expect? Can you > >> > provide a concrete example? > > >> > On Jan 7, 9:31 am, "KONTRA, Gergely" <pihent...@gmail.com> wrote: > >> >> Yes, AFAIK appadmin is a T2 thing. > > >> >> So, I wonder how can one rewrite the line below using > >> >> db.post.represent (still that is more flexible) > > >> >> db.define_table('post', > >> >> Field('title',length=256), > >> >> Field('body','text',requires=IS_NOT_EMPTY()), > >> >> Field('author',db.auth_user), > >> >> format='%(title)s') > > >> >> db.post.represent = ? > > >> >> thanks > >> >> Gergo > >
-- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.