The problem is that unlike departmentId, supervisorId is a self reference.
With a non-self-reference field, the DAL will automatically set the
"represent" attribute to be the _format attribute of the referenced table,
but that is not possible with a self reference because the referenced table
does not yet exist at the time the field is created. I think you'll have to
set the represent attribute separately in this case.
Anthony
On Wednesday, November 21, 2012 3:49:15 PM UTC-5, Jim S wrote:
>
> Yes, I know I can do that, but seems like this should be the default
> behavior for web2py. I'm just looking for it to work the same way the
> other reference fields work (see the departmentId field in my example).
>
> -Jim
>
> On Wednesday, November 21, 2012 1:59:22 PM UTC-6, Cliff Kachinske wrote:
>>
>> In your controller do something like
>> def get_name(id):
>> record = db(db.employees.id==id).select(db.employees.first_name, db.
>> employees.last_name).first()
>> return ' %s %s' %(record.first_name, record.last_name)
>>
>> db.employee.supervisor_id.represent = lambda row: get_name(row.
>> supervisor_id)
>>
>> If it's an index list you will get one database hit per row. Better to
>> use this trick on the edit or view pages.
>>
>>
>> On Wednesday, November 21, 2012 2:32:54 PM UTC-5, Jim S wrote:
>>>
>>> I have a table defined as follows:
>>>
>>> employee = db.define_table('employee',
>>> Field('employeeId', 'id', writable=False, label='Employee #'),
>>> Field('firstName', length=25, required=True, label='First Name',
>>> writable=False),
>>> Field('lastName', length=25, required=True, label='Last Name',
>>> writable=False),
>>> ...
>>> Field('departmentId', db.department, label='Department', writable=False
>>> ),
>>> Field('supervisorId', 'reference employee', label='Supervisor',writable
>>> =False),
>>> format='%(lastName)s, %(firstName)s')
>>>
>>> db.employee.dob.requires = IS_NULL_OR(IS_DATE('%m/%d/%Y'))
>>> db.employee.seniorityDate.requires = IS_NULL_OR(IS_DATE('%m/%d/%Y'))
>>> db.employee.hireDate.requires = IS_NULL_OR(IS_DATE('%m/%d/%Y'))
>>> db.employee.originalHireDate.requires = IS_NULL_OR(IS_DATE('%m/%d/%Y'))
>>> db.employee.terminationDate.requires = IS_NULL_OR(IS_DATE('%m/%d/%Y'))
>>>
>>>
>>>
>>>
>>>
>>> My supervisorId field displays correctly with the dropdown if it is
>>> writable, but when I set writable=False it just displays the value of the
>>> supervisorId field, not the assosiated employee first/last name as the
>>> format would dictate. departmentId is setup to behave the same way, just
>>> referencing a different table and it displays the proper 'name' of the
>>> department when writable=False instead of the id field like supervisorId
>>> does. Is this a bug?
>>>
>>> -Jim
>>>
>>
--