if not isinstance(test0, (tuple, list)): test0 = [test0]
query = (db.test1.test0.belongs(test0) )


On Friday, 28 July 2017 20:31:25 UTC-5, 黄祥 wrote:
>
> is it possible belongs to handle if there is only one value?
> e.g.
> *models/db.py*
> db.define_table('test0', 
> Field('name'),
> format = '%(name)s')
> db.define_table('test1', 
> Field('name'),
> Field('test0', 'reference test0'),
> format = '%(name)s')
>
> *controllers/default.py*
> def install():
> if db(db.test0).isempty() and db(db.test1).isempty():
> db.test0.bulk_insert([{'name' : 'test0 - a'}, {'name' : 'test0 - b'}, ] )
> db.test1.bulk_insert([{'name' : 'test1 - a', 'test0' : 1}, {'name' : 
> 'test1 - b', 'test0' : 2}, ] )
> def test0():
> form = SQLFORM.factory(
> Field("test0", "list:reference test0", 
>  requires = IS_EMPTY_OR(IS_IN_DB(db, db.test0.id, db.test0._format, 
>    multiple = True) ) ) )
> if form.process().accepted:
> redirect(URL('test1', vars = form.vars) )
> elif form.errors:
> response.flash = T('Form has errors')
> return dict(form = form)
> def test1():
> test0 = request.vars.test0
> query = (db.test1.test0.belongs(test0) )
> rows = db(query).select()
> return dict(test0 = test0, query = query, rows = rows)
>
> *result:*
> http://127.0.0.1:8000/a/default/test1?test0=1#
> query : 
> <Query (test1.test0 IN ())>
> rows : 
> test0 : 
> 1
>
> http://127.0.0.1:8000/a/default/test1?test0=1&test0=2#
> query : 
> <Query (test1.test0 IN (1,2))>
> rows : 
> test1.idtest1.nametest1.test0
> 1 test1 - a test0 - a
> 2 test1 - b test0 - b
> test0 : 
> 1
> 2
>
> *question:*
> why belongs cannot handle if the value of dict is only one value? how can 
> achieve it using web2py way?
>
> thanks and best regards,
> stifan
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to