In order to generate a model with foreign references to itself you need
to use'reference table' as opposed to db.table in the Field defines.
While this works, I noticed that drop down lists and id->name mapping
were missing from the generated grid. I initially thought this was due
to using the 'reference table' construct - but it seems not (see below),
I have an variant of the 'dogs' app that uses 'reference person' and
*those* id->name mapping and drop downs get created in the grid, but the
corresponding ones for sire_id are not.
If I add
db.dog.sire_id.requires = IS_EMPTY_OR(IS_IN_DB(db, 'dog.id'))
in the model then I get a drop down list for potential sire dogs for
editing - so I'm guessing I need to do something with
db.dog.sire_id.represent = ??
However shouldn't this happen automatically?
model
---------
db.define_table(
'person',
Field('name'),
Field('email'),
format = '%(name)s',
singular = 'Person',
plural = 'Persons',
)
db.define_table(
'dog',
Field('owner_id', 'reference person'),
Field('name'),
Field('type'),
Field('sire_id', 'reference dog'),
Field('picture', 'upload', default=''),
format = '%(name)s',
singular = 'Dog',
plural = 'Dogs',
)
db.person.name.requires = IS_NOT_EMPTY()
db.person.email.requires = [IS_EMAIL(), IS_NOT_IN_DB(db, 'person.email')]
db.dog.name.requires = IS_NOT_EMPTY()
db.dog.type.requires = IS_IN_SET(('small', 'medium', 'large'))
Controller
--------------
def register_dog():
form=SQLFORM.grid(db.dog, csv=False, paginate=5, user_signature=False)
return dict(form=form)
Regards
Mark