Actually, I would think that would have caused an AttributeError (trying to access an attribute of a NoneType object). Not sure where the KeyError is arising.
Anthony On Friday, July 20, 2012 4:31:52 PM UTC-4, Anthony wrote: > > Oh, right, since you allow empty values, you'll need to test for that > before doing the query: > > represent=lambda id, row: db.person(id).name if id else 'Nobody owns this > dog' > > Anthony > > On Friday, July 20, 2012 3:47:33 PM UTC-4, joe wrote: >> >> That hasn't worked for me. When I try, I get the error:<type >> 'exceptions.KeyError'> 'name' >> >> It points to the smartgrid I made, with this code: grid = >> SQLFORM<http://127.0.0.1:8000/examples/global/vars/SQLFORM> >> .smartgrid(db.dog) >> >> Thanks! >> -Joe Peacock >> >> >> On Tuesday, July 17, 2012 6:22:53 PM UTC-5, Anthony wrote: >>> >>> You should probably make it a reference field. By default, if you don't >>> specify any "requires", you'll automatically get an IS_IN_DB validator as >>> well as a "represent" attribute that displays whatever is specifying by the >>> "format" attribute of the referenced table. However, if you specify your >>> own validator(s), then you don't get any default "represent" -- so just add >>> your own explicit represent: >>> >>> db.define_table('dog', >>> Field <http://127.0.0.1:8000/examples/global/vars/Field>('name'), >>> Field <http://127.0.0.1:8000/examples/global/vars/Field>('owner', db >>> .person, >>> requires=IS_EMPTY_OR<http://127.0.0.1:8000/examples/global/vars/IS_EMPTY_OR> >>> (IS_IN_DB <http://127.0.0.1:8000/examples/global/vars/IS_IN_DB>(db, ' >>> person.id', '%(name)')), >>> represent=lambda id, row: db.person(id).name), >>> format = '%(name)s') >>> >>> Anthony >>> >>> On Tuesday, July 17, 2012 10:26:52 AM UTC-4, joe wrote: >>>> >>>> Is there any way to make a field in a form referencing a foreign key >>>> null, while storing a foreign key as a foreign key, but making dropdowns >>>> corrospond to the format. Here is my example: >>>> >>>> Model: >>>> >>>> db = DAL >>>> <http://127.0.0.1:8000/examples/global/vars/DAL>('sqlite://storage.sqlite') >>>> >>>> db.define_table('person', >>>> Field <http://127.0.0.1:8000/examples/global/vars/Field>('name'), >>>> Field <http://127.0.0.1:8000/examples/global/vars/Field>('email'), >>>> format = '%(name)s') >>>> >>>> db.define_table('dog', >>>> Field <http://127.0.0.1:8000/examples/global/vars/Field>('name'), >>>> Field <http://127.0.0.1:8000/examples/global/vars/Field>('owner', >>>> requires = IS_EMPTY_OR >>>> <http://127.0.0.1:8000/examples/global/vars/IS_EMPTY_OR>(IS_IN_DB >>>> <http://127.0.0.1:8000/examples/global/vars/IS_IN_DB>(db,db.person))), #I >>>> have also tried default=None, and required = False, no difference >>>> format = '%(name)s') >>>> >>>> >>>> Controller: >>>> >>>> *from gluon.tools import Crud >>>> crud = Crud(db) >>>> >>>> def index(): >>>> form = SQLFORM(db.person) >>>> if form.process().accepted: >>>> response.flash = 'success' >>>> return dict(form=form) >>>> >>>> def add_dog(): >>>> form = SQLFORM(db.dog) >>>> if form.process().accepted: >>>> response.flash = 'success' >>>> return dict(form=form) >>>> >>>> def view(): >>>> grid = SQLFORM.smartgrid(db.dog) >>>> grid2 = crud.select(db.person) >>>> return dict(grid=grid, grid2=grid2) >>>> * >>>> >>>> * >>>> * >>>> >>>> When I have the above code, a foreign key will be stored in the database, >>>> but all views of that key will be rendered as the numerical value of the >>>> foreign key, and not the name. In the table definition, if I just have >>>> *db.person* instead of requires = *IS_EMPTY_OR*(*IS_IN_DB*(db,db.person)), >>>> It requires the field. >>>> >>>> >>>> Are there any workarounds? >>>> >>>> -Joe Peacock >>>> >>>> --

