Hi,

First off, I've already fixed my problem by coding it differently.
However, I was curious as to the proper way to set up this validator
so it shows a dropdown.

I have two models, which can be simplified to:

db.define_table('booth',
 
Field('name','string',length=10,required=True,notnull=True,unique=True))
db.define_table('lease',
                Field('booth',db.booth),
                Field('end_date','date'))

When creating a new "lease", the SQLFORM field to pick a "booth" to
attach should only show booths which have no lease. So I added this in
the controller:

def new():
    db.lease.booth.requires = IS_NOT_IN_DB(db(db.lease.end_date ==
None)),'lease.booth')

This promptly transformed the booth dropdown into a text box. It
worked fine if, when testing, I used IS_IN_DB instead (showing only
booths with current leases, which is of course backwards, but it
showed them as a dropdown). I know that if you put the validators in a
list they will not create dropdowns, and I made sure that my validator
was not in a list.

So instead I switched to this:

def new():
    leased_booths = db(db.lease.end_date ==
None)._select(db.lease.booth)
    db.lease.booth.requires =
IS_IN_DB(db(~(db.booth.id.belongs(leased_booths))),'booth.id','%
(name)s')

which is a bit less efficient but displays the desired behavior. Is
there a more "web2py"-ish way to accomplish this, or is IS_NOT_IN_DB
not supposed to display a dropdown? From my reading of the
documentation, I thought it was supposed to do so.

Reply via email to