Append the same fields to all the tables

db._common_fields is a list of fields that should belong to all the tables. By adding code below to a model file, all tables will contain fields specified in _common_fields.

Model db.py


Copy below code to your model file before first defined table.






















|True|# coding: utf8


signature = db.Table(db,'auth_signature',
Field('created_on','datetime',default=request.now,
writable=False,readable=False, label=T('Created on')),
Field('created_by','reference %s' % auth.settings.table_user_name,default=auth.user_id,
writable=False,readable=False, label=T('Created by')),
Field('modified_on','datetime',update=request.now,default=request.now,
writable=False,readable=False, label=T('Modified on')),
Field('modified_by','reference %s' % auth.settings.table_user_name,
default=auth.user_id,update=auth.user_id,
writable=False,readable=False, label=T('Modified by'))
)

db._common_fields.append(signature) #db._common_fields is a list of fields that should belong to all the tables

db.define_table('category',
Field('is_enabled', 'boolean', default=True, label=T('Enabled')),
Field('name', 'string', length=256, label=T('Name')),
Field('description', 'text', readable=True, writable=True, label=T('Description')),
format='%(name)s'
)|

Result

db._common_fields

loading...

Comments

loading...

Tag Cloud

accepted Acces Control access rights actions_disabled admin allowed_attributes allowed_schemes App apps args auth authentication B bar chart BEAUTIFY begin BODY BR carousel CAT CENTER chart check CLEANUP client request client_side CMS CODE COL COLGROUP comment compute contact form crud.create crud.delete crud.read crud.select crud.tables CRUD custom attributes custom form DAL data define_table deletable depriciated DIV doctype dot dspval EM embed64 enable_record_version end error_message example extension Field types Field FIELDSET file filename form.accepted form.accepts form.process form.validate FORM format Forms Grid H1 H2 H3 H4 H5 H6 hash_vars Hello helper helpers highchart hmac_key host hosting HTML image images inpval ip address ip IS_DATETIME IS_DATETIME_IN_RANGE IS_DECIMAL_IN_RANGE IS_EMPTY_OR IS_EQUAL_TO IS_EXPR IS_FLOAT_IN_RANGE IS_IMAGE IS_INT_IN_RANGE IS_IN_DB IS_IN_SET IS_IPV4 IS_LIST_OF IS_NOT_EMPTY IS_NOT_IN_DB IS_NULL_OR is_slug IS_STRONG IS_TIME IS_UPLOAD_FILENAME IS_UPPER IS_URL lable lambda lang lastdot linkto li_class li_first li_last login_methods maximum maxip maxlen MENU min minimum minip mobile mode module multiple permitted_tags pie chart placeholder plugin prepend_scheme pythonanywhere query rating reCaptcha redirect request.now routes.py salt sanitize scheme services settings slug sortable special SQLFORM() SQLFORM.grid SQLFORM SSL submit table constructor TAG test text TinyWebsite ul_class update upper URL rewrite URL validator value vars Views widget XML xmlescape _class _common_fields


Powered by
Web2py

Hosted on
www.pythonanywhere.com
(affiliated link)