Monday, November 21, 2011

web2py: add additonal field in auth_user

It's actually very easy. Just add the following code in your model before auth.define_tables().



## additonal fields
auth.settings.extra_fields['auth_user']= [
  Field('office')
  ]

So it will be like this...

from gluon.tools import Auth, Crud, Service, PluginManager, prettydate
auth = Auth(db, hmac_key=Auth.get_or_create_key())
crud, service, plugins = Crud(db), Service(), PluginManager()

## create all tables needed by auth if not custom tables
## additonal fields
auth.settings.extra_fields['auth_user']= [
  Field('office')
  ]
auth.define_tables()

3 comments:

  1. Hmm, I just added this this to db.py:

    auth.settings.extra_fields['auth_group']= [
    Field('user_type', type='integer')
    ]

    but I don't see the new column in the database

    ReplyDelete
    Replies
    1. Woops! You can add it before "auth.define_tables()".

      It will be like...

      from gluon.tools import Auth, Crud, Service, PluginManager, prettydate
      auth = Auth(db, hmac_key=Auth.get_or_create_key())
      crud, service, plugins = Crud(db), Service(), PluginManager()

      ## create all tables needed by auth if not custom tables
      auth.settings.extra_fields['auth_group']= [
      Field('user_type', type='integer')
      ]
      auth.define_tables()

      Delete
  2. I had to make sure that migrate=True was turned on after adding a phone field to my auth_user table.

    auth.define_tables(username=True, signature=False,migrate=True)

    ReplyDelete