Adding custom attributes to fields and tables

62 views
Skip to first unread message

xelomac

unread,
Apr 22, 2018, 6:15:03 AM4/22/18
to web2py-users

I want to add a custom attribute so that I can decide which customwidget has to be used for a form field connected to that field.

Example:

db.define_table('person',
   
Field('salutation', mycustomwidget='select2', label = T('Salutation'), requires=IS_IN_SET(GENDER), represent=lambda v, r: GENDER[v]),
   
Field('first_name', label = T('First Name')),
    format
= '%(last_name)s')


I had no luck defining that custom attribute the way it is described in the book. How and and where would I define such a custom attribute 'mycustomwidget' for the table 'person' or even better for all tables?


Adding attributes to fields and tables

If you need to add custom attributes to fields, you can simply do this:

1
db.table.field.extra = {}

"extra" is not a keyword ; it's a custom attributes now attached to the field object. You can do it with tables too but they must be preceded by an underscore to avoid naming conflicts with fields:

1
db.table._extra = {} 

Anthony

unread,
Apr 22, 2018, 11:22:08 AM4/22/18
to web2py-users
First, what happens when you try:

db.person.salutation.mycustomwidget = 'select2'

Second, what are you really trying to do? How do you intend to use that custom attribute exactly?

Anthony

jim kaubisch

unread,
Jul 30, 2019, 3:33:00 PM7/30/19
to web2py-users
Similar question, but maybe simpler?

I have a table name which, for various reasons, isn't very human reader friendly (crowner). I'd like to add a table attribute (e.g. print_name = 'Copyright Owners') to be used for table display instead of the table name itself
Does that mean db.table._extra = {'print_name' : 'Copyright Owners'} or ??? If so, how do I access that attribute? db.table.['print_name'] ???
Thanks
Reply all
Reply to author
Forward
0 new messages