Given a table xyz that I have defined via db.define_table('xyz'..)
that I can reference as db.xzy, how can I add a new field to this already defined table object?
The usage case that I'm thinking off is I want to add a created
field to several tables. I also want to avoid repeating myself. My thought was to create a function that takes a table and adds the created
field to it. So for example:
def addcreated(table):
# ??? somehow add a new Field('created', 'datetime') to table
table.created.writable = False
table._before_insert.append...
... etc.
db.define_table('product',
Field('name','string'),
Field('weight','double')
)
db.define_table('customer',
Field('name','string'),
Field('address','string')
)
addcreated(db.product)
addcreated(db.customer)
created_field = Field('created', 'datetime', writable=False)
db.define_table('product',
Field('name','string'),
Field('weight','double'),
created_field)
or:
db.define_table('product',
Field('name','string'),
Field('weight','double'),
created_field.clone(...))
With .clone()
you get a copy of the Field
object and can specify a new set of arguments to change some of its attributes.
For re-use of multiple fields, see the documentation on table inheritance.