Thank you!
On Feb 17, 8:08 pm, Anthony <abasta...@gmail.com> wrote: > > Hi, I'm going through the web2py tutorial and I don't understand part > > of this line: > > > db.comment.image_id.requires = IS_IN_DB(db, db.image.id, '%(title)s') > > > I understand that this requires that the image ID of the image the > > comment is under should be in the database. However, I don't > > understand what the '%(title)s' at the end is doing. > > That is Python string formatting syntax -- > seehttp://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html.... > It is saying to represent each db.image record with the "title" field > rather than the "id" field. Each db.image row object acts like a > dictionary, so when you do: > > '%(title)s' % db.image[1] > > that will extract the "title" field value from the db.image[1] record. The > IS_IN_DB validator uses this method to generate options to be shown in > select widgets in forms. So, even though the validator is checking for > values of the db.image.id field, the widget in the user interface shows the > db.image.title values instead. > > Note, instead of specifying '%(title)s', you can also specify a function > (including a lambda function) that takes the db.image row and returns > whatever representation you want. > > Anthony