[web2py] Re: Self Referential references in models not getting id decoded automatically
I see that this has been noted as an issue here: http://code.google.com/p/web2py/issues/detail?id=382 I am using 1.99.4, anything I can try? Regards Mark On 05/02/12 13:17, Mark Kirkwood wrote: 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?
[web2py] Re: Self Referential references in models not getting id decoded automatically
It *almost* works using: db.dog.sire_id.represent = lambda sire_id, row: db(db.dog.id==sire_id).select(db.dog.name) However that brings back a header 'dog.name' that mucks up the appearance of the form. What I wanted to write was: db.dog.sire_id.represent = lambda sire_id, row: db.dog(sire_id).name if sire_id != None else None However that produces: AttributeError: 'NoneType' object has no attribute 'name' Which stumps me I must say, since I can clearly see a 'name:' entry displayed if I just leave off the '.name' in the code above... Any suggestions welcome regards Mark On 05/02/12 13:24, Mark Kirkwood wrote: I see that this has been noted as an issue here: http://code.google.com/p/web2py/issues/detail?id=382 I am using 1.99.4, anything I can try?
[web2py] Re: Self Referential references in models not getting id decoded automatically
Ok figured out what was causing that - I had one record with '0' for a sire_id and no dog with id = 0. Fixing that up makes the representation code below work. Yay! *But*... now the edit drop down list is showing id's instead of names... scratches head I'm sure that *was* working before I even redid the example from scratch and...still seeing id's instead of names in edit mode. Blast need to try again tomorrow and see if I can sort that. Well at least figured out one of the problems! On 05/02/12 15:51, Mark Kirkwood wrote: However that brings back a header 'dog.name' that mucks up the appearance of the form. What I wanted to write was: db.dog.sire_id.represent = lambda sire_id, row: db.dog(sire_id).name if sire_id != None else None However that produces: AttributeError: 'NoneType' object has no attribute 'name' Which stumps me I must say, since I can clearly see a 'name:' entry displayed if I just leave off the '.name' in the code above...
[web2py] Re: Self Referential references in models not getting id decoded automatically
You could do: def register_dog(): if request.args(0) == 'edit': db.dog.sire_id.requires = IS_IN_DB(db(db.dog.id! =request.args(2)), db.dog, db.dog._format) form=SQLFORM.grid(db.dog, csv=False, paginate=5, user_signature=False) return dict(form=form) On Feb 5, 4:23 am, Mark Kirkwood wrote: > Ok figured out what was causing that - I had one record with '0' for a > sire_id and no dog with id = 0. Fixing that up makes the representation > code below work. Yay! *But*... now the edit drop down list is showing > id's instead of names... scratches head I'm sure that *was* working > before > > I even redid the example from scratch and...still seeing id's instead of > names in edit mode. Blast need to try again tomorrow and see if I can > sort that. Well at least figured out one of the problems! > > On 05/02/12 15:51, Mark Kirkwood wrote: > > > > > > > > > > > However that brings back a header 'dog.name' that mucks up the > > appearance of the form. What I wanted to write was: > > > db.dog.sire_id.represent = lambda sire_id, row: db.dog(sire_id).name > > if sire_id != None else None > > > However that produces: > > > AttributeError: 'NoneType' object has no attribute 'name' > > > Which stumps me I must say, since I can clearly see a 'name:' entry > > displayed if I just leave off the '.name' in the code above...
[web2py] Re: Self Referential references in models not getting id decoded automatically
A simpler fix for the edit/add functions is to specify a format in the model: db.dog.sire_id.requires = IS_EMPTY_OR(IS_IN_DB(db, 'dog.id', '%(name)s')) (Hmm ... suspect I had that there before at some stage which, but edited the format away when I was trying to pin down the other problems - oops). Anyway, thanks for the help! Mark On 05/02/12 22:23, Mark Kirkwood wrote: *But*... now the edit drop down list is showing id's instead of names... scratches head I'm sure that *was* working before