> > > You cannot do 'id IN' because GAE treats ID in a spacial way. Please
> > > try a different field.

> > I can run a separate "=" query for each of the elements that I want to
> > include in an "IN" condition. Not efficient, but it should work.

> Can you show me an example of how you do it?

Hi Massimo-
Here's an example. I'm using a slightly different data set than what I
described before, so allow me to re-introduce the problem.

I'm using GAE and also testing on SQLite. I have 2 tables in the
datastore, which I'm using to store information about people and their
pets. They would be defined like this in the model, which overrides
the default auth user table and has another table for the
relationships between pet owners and pets:

auth.settings.table_user = db.define_table(  ## stores information
about people and pets
    auth.settings.table_user_name,
    db.Field('name', length=32,default='',
        requires=[IS_NOT_EMPTY(),IS_NOT_IN_DB
(db,'%s.name'%auth.settings.table_user_name)]),
    db.Field('is_human', 'boolean', default=True, ## for pets, this
should be set to "False"
        writable=False, readable=False, requires=IS_NOT_EMPTY()),
    db.Field('email', length=128,default='',
        requires=[IS_NOT_EMPTY(),IS_EMAIL(),
                  IS_NOT_IN_DB
(db,'%s.email'%auth.settings.table_user_name)]),
    db.Field('password', 'password', readable=False, label='Password',
requires=CRYPT()),
    db.Field('registration_key', length=128, writable=False,
readable=False,default=''),
    )

db.define_table('relationships',
    SQLField('person_id',auth.settings.table_user,
        requires=IS_IN_DB(db
(auth.settings.table_user.is_human=="True"),
        '%s.id'%auth.settings.table_user_name,
        '%s.name'%auth.settings.table_user_name)),
    SQLField('pet_id',auth.settings.table_user,
        requires=IS_IN_DB(db
(auth.settings.table_user.is_human=="False"),
        '%s.id'%auth.settings.table_user_name,
        '%s.name'%auth.settings.table_user_name)),
)

Then in the controller, I want to show each user a form which will
have a select input allowing the user to pick one of the pets that he
owns. And I want to validate the form variables to make sure that the
selected pet is owned by the user. I'm using auth and requiring the
user to be logged in. So here is the code:

@auth.requires_login()
def report_form():
    # first run a query to find out which pets the current user owns
    pet_ids_allowed = db
(db.relationships.person_id==auth.user.id).select()
    # then run a query to get the name of each of the pets owned
    # for GAE compatibility, we will run 1 query for each pet.
    # If this was running on SQLite, we could use a belongs/IN clause
and get the names of all the pets in one query
    petslookup = {} # associate userid with each pet. we'll use this
for validation
    for p in pet_ids_allowed:
        petname = db(db.auth_user.id==p['pet_id']).select()[0]['name']
        petlookup[petname] = p['pet_id']

    form=FORM(TABLE(
        TR("Pet Name:",SELECT(_name='petname',
            requires=[IS_NOT_EMPTY(),IS_IN_SET(petlookup.keys())],
            *[OPTION(k, _value=k) for k in petlookup.keys()]
            )),
        TR("",INPUT(_type="submit",_value="Submit")) ))

    if form.accepts(request.vars,formname=None):
        response.flash="form accepted input"
        pet_id = petlookup[petname]
        actionsummary = "this is where we would do something with the
form input."
    elif form.errors:
        response.flash="form is invalid"
        actionsummary="Information received was invalid. Nothing
done."


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to