Add extra fields to db.auth_user

914 views
Skip to first unread message

Gael Princivalle

unread,
Dec 3, 2013, 10:14:02 AM12/3/13
to web...@googlegroups.com
Hi.

I've had extra fields to db.auth_user like that in db.py:

auth = Auth(db)
auth.settings.extra_fields['auth_user']= [Field('Company', requires=IS_NOT_EMPTY()),Field('Phone')]

Problem n°1, requires=IS_NOT_EMPTY() don't have any effect. A user can register without filling this field.

Problem n° 2, Company and Phone fields are in the form after passwords field, I would like to change the fild order.

How can I do it ?

Thanks.

James Burke

unread,
Dec 3, 2013, 1:56:39 PM12/3/13
to web...@googlegroups.com
For problem no1, you could try using required=True instead of requires=IS_NOT_EMPTY()

For your second problem you can define the auth_user table yourself (from web2py book):

Another way to do this, although not really recommended, consists of defining your auth tables yourself. If a table is declared before auth.define_tables() it is used instead of the default one. Here is how to do it:

## after auth = Auth(db)
db.define_table(
    auth.settings.table_user_name,
    Field('first_name', length=128, default=''),
    Field('last_name', length=128, default=''),
    Field('email', length=128, default='', unique=True), # required
    Field('password', 'password', length=512,            # required
          readable=False, label='Password'),
    Field('address'),
    Field('city'),
    Field('zip'),
    Field('phone'),
    Field('registration_key', length=512,                # required
          writable=False, readable=False, default=''),
    Field('reset_password_key', length=512,              # required
          writable=False, readable=False, default=''),
    Field('registration_id', length=512,                 # required
          writable=False, readable=False, default=''))

## do not forget validators
custom_auth_table = db[auth.settings.table_user_name] # get the custom_auth_table
custom_auth_table.first_name.requires =   IS_NOT_EMPTY(error_message=auth.messages.is_empty)
custom_auth_table.last_name.requires =   IS_NOT_EMPTY(error_message=auth.messages.is_empty)
custom_auth_table.password.requires = [IS_STRONG(), CRYPT()]
custom_auth_table.email.requires = [
  IS_EMAIL(error_message=auth.messages.invalid_email),
  IS_NOT_IN_DB(db, custom_auth_table.email)]

auth.settings.table_user = custom_auth_table # tell auth to use custom_auth_table

## before auth.define_tables()

Gael Princivalle

unread,
Dec 4, 2013, 9:48:33 AM12/4/13
to web...@googlegroups.com
Thanks James but also required=True don't have any effect. Perhaps I have to make a custom form or register, do you know I can do it ?

Massimo Di Pierro

unread,
Dec 4, 2013, 10:29:14 AM12/4/13
to web...@googlegroups.com
I cannot reproduce problem 1. There is nothing wrong in your code and something else is causing the problem. Are you sure the issue is that "A user can register without filling this field." or is it the users who registered before the contraint was introduced do not have a company field?

Problem 2. To fix that you need define your own auth_user table. If you do:

db.define_table('auth_user', ....)

before auth.define_tables() it should use yours.

Gael Princivalle

unread,
Dec 4, 2013, 11:56:06 AM12/4/13
to web...@googlegroups.com
Thanks a lot Massimo, in fact I've made testing on an existing user. Now with requires=IS_NOT_EMPTY() the form ask to fill the company field. Perfect.

For problem 2 I'm gone define my own auth_user table, have a nice day.

Ron Chatterjee

unread,
Jan 30, 2016, 9:41:27 PM1/30/16
to web...@googlegroups.com
Beside adding extra field, is it possible to split the table for auth_user? For example, I want the profile to be broken in different tables based on address, education, portfolio...etc. that someone can create a profile. because following whats in the custom table. It will create one huge table for registration. What if it can be broken in different pieces.
Reply all
Reply to author
Forward
0 new messages