Hello
I have a question regarding adding extra fields to auth_user table
now I saw a couple of posts but none answers my question
I have a table organization. On user registration user adds organization
name in form. this value should be inserted into table organization and id
of the record into auth_user.organization field.
Also some other default values should be inserted into another config table.
I had this working now I am getting an error
<class 'psycopg2.IntegrityError'> insert or update on table "auth_user"
violates foreign key constraint "auth_user_organization_fkey" DETAIL: Key
(organization)=(0) is not present in table "organizations".
my db.py code
db.define_table('organizations',
Field('id',type='id'),
Field('o_daten', type='datetime',default=request.now),
Field('o_dateu', type='datetime',default=request.now),
Field('o_status',type='integer' ),
Field('o_code',type='string', length = 64 ),
Field('o_name',type='string', label= T('Organization name'), length =
256),
Field('o_telnumber',type='string',label= T('Telephone number'), length
= 64 ),
Field('o_faxnumber',type='string',label= T('Fax Nb.'), length = 64 ),
Field('o_street',type='string',label= T('Street'), length = 64 ),
Field('o_post',type='string',label= T('Post'), length = 64 ),
Field('o_city',type='string',label= T('City'), length = 64 ),
Field('o_state',type='string',label= T('State'), length = 64 ),
Field('o_country',type='string',label= T('Country'), length = 64 ),
Field('o_TaxNumber',type='string',label= T('Tax number'), length = 64 ),
Field('o_rootid',type='integer' ),
Field('o_parentid',type='integer' ),
Field('o_levelid',type='integer'),
Field('o_positionx',type='string', length = 64 ),
Field('o_positiony',type='string', length = 64 ),
Field('o_note',label= 'Note',type = 'text'),
migrate=settings.migrate
)
then extra field, autocomplete is not really necessary
auth.settings.extra_fields['auth_user'] = [ Field('organization',
'reference organizations',
widget=SQLFORM.widgets.autocomplete(request, db.organizations.o_name,
id_field=db.organizations.id),
label =
T('Organization'))]
my function to add organization and rows in config table
def add_organization(form):
"""
Always adds new organization
"""
# if not form.vars.organization:
# print "form.vars ",form.vars
ret =
db.organizations.validate_and_insert(o_name=form.vars._autocomplete_organization_o_name_aux)
if ret.errors:
form.errors.organizations = ret.errors['name']
else:
org = ret.id
"""
Dodamo podatke v config tabelo ko se kreira nova organizacija
holiday_duration 1 480 portal Default trajanje praznika
vacation_duration 1 480 portal Default trajanje dopusta
holiday_color 1 008000 portal temno zelena
vacation_color 1 00ff00 portal svetlo zelena
"""
db.config.insert(co_code = "holiday_duration", co_value = '420',
co_organisation = org, co_note = 'Default trajanje praznika')
db.config.insert(co_code = "vacation_duration", co_value = '480',
co_organisation = org , co_note = 'Default trajanje dopusta')
db.config.insert(co_code = "holiday_color", co_value = '008000 ',
co_organisation = org, co_note = 'Default barva holiday temno zelena')
db.config.insert(co_code = "vacation_color", co_value = '00ff00 ',
co_organisation = org, co_note = 'Default barva vacation svetlo zelena')
db.config.insert(co_code = "minimum_time_period", co_value = '15 ',
co_organisation = org, co_note = 'Default vrednost Ĩasovne periode')
#if not form.vars.organizations:
# print form.vars._autocomplete_organizations_o_name_aux
org_name = form.vars._autocomplete_organizations_o_name_aux
ret = db.organizations.insert(o_name = org_name)
and at the end
auth.settings.register_onvalidation = add_organization
## create all tables needed by auth if not custom tables
auth.define_tables(username=False, signature=False)
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.