using contains operator with reference types

58 views
Skip to first unread message

dave

unread,
Aug 8, 2013, 3:28:47 PM8/8/13
to web...@googlegroups.com
I have two tables defined as follows

db.define_table('animals', 
            Field('type'),
            format='%(type)s')

db.define_table('zoo', 
            Field('name'), 
            Field('tier', 'reference animals'),
            format='%(name)s' 
            ) 

field type is a column with values like, test 1, test 2, test 3
now if I want to select all the records of table zoo with 'test 2' I can do something like this 

rows = db(db.zoo.tier == "2").select()

but why can't I do something like 
db(db.zoo.tier.contains("2")).select() or
pass a list  ["2", "3"] to the contains operator to get all the records of "test 2" and "test 3"? can you suggest another way of implementing this?

Massimo Di Pierro

unread,
Aug 8, 2013, 3:51:12 PM8/8/13
to web...@googlegroups.com
This

rows = db(db.zoo.tier == 2).select()

is equivalent to

rows = db(db.zoo.tier.belongs([2])).select()

you can do

rows = db(db.zoo.tier.belongs([2, 3])).select()

dave

unread,
Aug 8, 2013, 4:19:47 PM8/8/13
to web...@googlegroups.com
ok that works but I have one other question, how would I use the belongs operator If I want to refer a field by a name for example
instead of rows = db(db.zoo.tier.belongs([2, 3])).select()  I want to do this
               rows = db(db.zoo.tier.belongs(["test 2", "test 3"])).select()  ?

Niphlod

unread,
Aug 8, 2013, 4:55:48 PM8/8/13
to web...@googlegroups.com
you join them first and then you belong() on the type column.

db(
    (db.zoo.tier == db.animals.id) &
    (db.animals.type.belongs(['test1', 'test2']))
).select(db.zoo.ALL)

dave

unread,
Aug 8, 2013, 6:36:11 PM8/8/13
to web...@googlegroups.com
is there another way to do it, because this replaces my db.zoo.tier field with the id

Niphlod

unread,
Aug 9, 2013, 6:10:26 AM8/9/13
to web...@googlegroups.com
ehm... mine was an example. You can include in the select what you want from the animals table....

dave

unread,
Aug 9, 2013, 10:37:04 PM8/9/13
to web...@googlegroups.com
Ok got it thank you very much
Reply all
Reply to author
Forward
0 new messages