Hi:

I'm working with a SingleSelectField where I'd like to limit the values 
that are going into the select list.  Here is my situation:

I have users and plants.  A plant can have multiple users and a user can 
be in multiple plants.  I want a dropdown list of all of the plants for 
a specified user.  The model for the user and plant objects follow:

class User(SQLObject):
    class sqlmeta:
        table = "tg_user"

    user_name = UnicodeCol(length=16, alternateID=True, 
alternateMethodName="by_user_name")
    email_address = UnicodeCol(length=255, alternateID=True, 
alternateMethodName="by_email_address")
    display_name = UnicodeCol(length=255)
    password = UnicodeCol(length=40)
    brillLogon = UnicodeCol(length=10, dbName='brillLogon')
    active = BoolCol()
    created = DateTimeCol(default=datetime.now)
    # groups this user belongs to
    groups = RelatedJoin("Group", intermediateTable="user_group",
                         joinColumn="user_id", otherColumn="group_id")
    plants = RelatedJoin('Plant', intermediateTable='userPlant', 
joinColumn='userId', otherColumn='plantId')
   
class Plant(SQLObject):
    class sqlmeta:
        style = Style(longID=True)
        idName = 'plantId'
       
    plantNumber = IntCol()
    name = UnicodeCol(length=50)
    initials = UnicodeCol(length=3)
    active = BoolCol(default=True)
    users = RelatedJoin('User', intermediateTable='userPlant', 
joinColumn='plantId', otherColumn='userId')

I normally fill my dropdowns with the getXxxxOptions as detailed in the 
TurboGears book.  As such, the following is my code to fill the plant 
dropdown (when not limited by user)

def getPlantOptions():
    plants = Plant.select(orderBy='plantNumber')
    options = [(0, '-- None --')]
    for plant in plants:
        options.append((plant.id, plant.name))
    return options

But, what I really want to do is pass a userId into this method as a 
parameter so I'm only putting in the plants for a specific user.  Is 
there a way to do this?  If so, can someone point me in the right 
direction to figure out how I can do it?  What I'd really like to do is:

def getPlantOptions(userId=0):
    if userId != 0:
       u = User.get(userId)
       plants = u.plants
    else:
        plants = Plant.select(orderBy='plantNumber')
    for plant in plants:
        options.append((plant.id, plant.name))
    return options

But, then I wouldn't know how to specify it on my field def.  Here is 
what I do for the non-filtered dropdown:

plant = SingleSelectField(label='Plant:', 
options=qlfOptions.getUserPlantOptions)

I don't know where I'd supply the parameter.  Would I do it somehow in 
the controller?

    -Jim


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to