Thanks mr.freeze!! That got me in the right direction.

The dept_id comes from long time with Oracle and M$ SQLServer, also I thought it would be nice to make the database structure easily compatible with other platforms. I don't know about PostgreSQL but from what I understand, MySQL doesn't automatically implement row_id like SQLite? Anyways I will just structure for SQLite and worry about other platforms later.

Actually another quick question, can one implement triggers/procedures to SQLite/MySQL from web2py? I notice that web2py appadmin runs some procedure to delete parent records. Following my example below, using the appadmin, if I delete create dept_name = 'Management' & create an employee = 'Bob Johnson, Management'. Then delete 'Management' from db.department it will also delete Bob Johnson from employee (and other record with relation to 'Management'). It would be nice to implement a procedure that instead of deleting record from employee it would update employee.dept_id = None. Is such possible?




On 06/08/2010 12:38 AM, mr.freeze wrote:
I don't think your model is not setup with the correct reference
fields. Try this:

db.define_table('department',
     Field('dept_id'),
     Field('dept_name'),format='%(name)s')
db.define_table('employee',
     Field('firstName'),
     Field('lastName'),
     Field('dept_id', db.department))

Passing the format parameter to the department table will create the
default validators and represent attributes so that it shows the name
instead of the id.

The dept_id field on the department table seems redundant as the table
will already have an ID field automatically.


On Jun 7, 9:24 am, Jason Lotz<jayl...@gmail.com>  wrote:
Creating a custom crud.read form and I would like to show the
field.requires=IS_IN_DB field rather than the table.id . Sorry if that
doesn't make much sense. I have two related tables.

db.define_table('employee', Field('firstName'), Field('lastName'),
Field('dept_id'))
db.define_table('department', Field('dept_id'), Field('dept_name'))
db.employee.dept_id.requires=IS_NULL_OR(IS_IN_DB(db,'department.dept_id','department.dept_name'))

I would like to make a custom form to show the employee name and
department.

def employee_form():
   form = crud.read(db.employee, request.args[0])
   return dict(form=form)

## View
{{=form.custom.begin}}
First Name: {{=form.custom.widget.firstName}}
Last Name: {{=form.custom.widget.lastName}}
Department: {{=form.custom.widget.dept_id}}
{{=form.custom.end}}

This returns the actual dept_id for the Department, but I would like
to return the dept_name. Is there a better method to accomplish this
or can some explain custom widgets. I have read the book for custom
widgets but didn't completely understand it.

Reply via email to