If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!


  • 0
    pwoolf  15 years ago
    1. following is a way to define a field group that can be appended to table definitions. Note that unique_id is string, not boolean as shown above. I added a field, 'reason', for my own purposes. #audit field group audit=db.Table(None,'audit', Field('unique_id', default=uuid.uuid4(), writable=False, readable=False), Field('active', 'boolean', default=True, comment='set Active off to delete record'), Field('created_by', db.auth_user, default=user_id, writable=False), Field('created_on', 'datetime', default=request.now, writable=False), Field('modified_by', db.auth_user, default=user_id, update=user_id, writable=False), Field('modified_on', 'datetime', default=request.now, update=request.now, writable=False), Field('reason', requires=IS_IN_SET(('change', 'new info', 'correction'))) ) db.define_table('home', Field('city'), audit, ) #Note that the validation of the reference from person to home below requires a filter on active rows only. db.define_table('person', Field('firstname'), Field('lastname'), Field('home', db.home, requires=IS_IN_DB(db(db.home.active==True), 'home.id', 'home.city')), audit, ) 2. A record delete for an audit-ed table should be logical only, achieved by setting active to False, & not allowing actual record deletion. Include deletable=False, in the crud.update to suppress delete checkbox. 3. You can use named scopes to separate out the selection of active records from other selection criteria to make the code clearer: person_active=db.person.active==True # required once only in controller def person_list_S(): rows=db(person_active)(db.person.lastname.like('S%')).select() return dict(rows=rows) #or, active=db(db.person.active==True), then rows=active(db...) ?The update fn shown in the slice does not show the updated record, even though the update is executed correctly.

Commented on:

draft

Hosting graciously provided by:
Python Anywhere