>
> In line 14, db.image.title represents the field "title" of table 
> "image". The 
> attribute requires allows you to set requirements/constraints that 
> will be 
> enforced by web2py forms. Here we require that the "title" is unique: 
> IS_NOT_IN_DB(db, db.image.title) 
> Notice this is optional because it is set automatically given that 
> Field(’title’, 
> unique=True).
>

The book says that explicitly adding the IS_NOT_IN_DB validator is optional 
because if you specify unique=True but do not explicitly specify any 
validators at all, the IS_NOT_IN_DB validator will be assigned 
automatically. For example:

>>> db.define_table('user', Field('username', unique=True))
>>> print db.user.username.requires
[<gluon.validators.IS_NOT_IN_DB object at 0x000000000439D710>,
<gluon.validators.IS_LENGTH object at 0x000000000439D5F8>]

Notice that I did not specify the IS_NOT_IN_DB validator in the field 
definition, but it was added anyway because I set unique=True. Note, this 
only happens if you don't specify "requires" at all.

Anthony

Reply via email to