> db.RelationshipSubjectArea.subjectAreaID.requires = IS_IN_DB(db, 
> 'SubjectArea.id', '%(subjectAreaCode)s',zero=T('choose one'))
> db.RelationshipSubjectArea.subjectAreaID.represent = lambda id, r: 
> '%(subjectAreaCode)s'  % db.SubjectArea(id)
>

You can remove the above two lines and instead just do:

db.define_table('SubjectArea',
    Field('subjectAreaCode','string', label='Subject Area Code'),
    format='%(subjectAreaCode)s')

That will give you default "requires" and "represent" attributes equivalent 
to the ones you have defined above.
 

> db.RelationshipRole.relationshipSubjectAreaID.requires = IS_IN_DB(db, 
> 'RelationshipSubjectArea.subjectAreaID', '%(subjectAreaID)s',zero=T('choose 
> one'))
>

Since the relationshipSubjectAreaID field references the 
db.RelationshipSubjectArea table, the validator should require that the 
value be in the "id" field of that table, not in the "subjectAreaID" field. 
The label argument should then use the subjectAreaID from that table to 
look up the subjectAreaCode from the db.SubjectArea table:

db.RelationshipRole.relationshipSubjectAreaID.requires = IS_IN_DB(db, 
'RelationshipSubjectArea.id',
    lambda r: db.SubjectArea(r.subjectAreaID).subjectAreaCode, zero=T('choose 
one'))

Or using a recursive select:

db.RelationshipRole.relationshipSubjectAreaID.requires = IS_IN_DB(db, 
'RelationshipSubjectArea.id',
    lambda r: r.subjectAreaID.subjectAreaCode, zero=T('choose one'))


db.RelationshipRole.relationshipSubjectAreaID.represent = lambda id, r: 
> '%(subjectAreaID)s'  % db.RelationshipSubjectArea(id)
>

The "represent" attribute does not propagate across multiple linked tables, 
so you have to be explicit:

db.RelationshipRole.relationshipSubjectAreaID.represent = \
    lambda id, r: db.SubjectArea(db.RelationshipSubjectArea(id).
subjectAreaID).subjectAreaCode

Or using recursive selects:

db.RelationshipRole.relationshipSubjectAreaID.represent = \
    lambda id, r: id.subjectAreaID.subjectAreaCode

Note, both of the above do two db queries per record. To get that to one 
per record, you can instead do a join:

db.RelationshipRole.relationshipSubjectAreaID.represent = \
    lambda id, r: db((db.RelationshipSubjectArea.id == id) &
                     (db.RelationshipSubjectArea.subjectAreaID == db.
SubjectArea.id))\
                  .select().first().SubjectArea.subjectAreaCode

Anthony

-- 

--- 
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/groups/opt_out.


Reply via email to