Patch to make belongs() a little nicer

21 views
Skip to first unread message

Michael Toomim

unread,
Jun 17, 2012, 11:46:20 PM6/17/12
to web2py-d...@googlegroups.com
This patch lets you use belongs() with fewer parentheses. Instead of having to type:

field.belongs((1,2))

You can now use:

field.belongs(1,2)

It's backwards compatible. Any objections? Can this go in trunk?

--- a/web2py/gluon/dal.py
+++ b/web2py/gluon/dal.py
@@ -7111,7 +7111,18 @@ class Expression(object):
     def regexp(self, value):
         return Query(self.db, self.db._adapter.REGEXP, self, value)
 
-    def belongs(self, value):
+    def belongs(self, *value):
+        '''
+        Accepts the following inputs:
+           field.belongs(1,2)
+           field.belongs((1,2))
+           field.belongs(query)
+
+        Does NOT accept:
+           field.belongs(1)
+        '''
+        if len(value) == 1:
+            value = value[0]
         if isinstance(value,Query):
             value = self.db(value)._select(value.first._table._id)
         return Query(self.db, self.db._adapter.BELONGS, self, value)

Michael Toomim

unread,
Jun 17, 2012, 11:50:16 PM6/17/12
to web2py-d...@googlegroups.com
To make this more concrete, it lets you run:

db(db.scheduler_task.status.belongs('QUEUED','ACTIVE')).select()
instead of
db(db.scheduler_task.status.belongs(('QUEUED','ACTIVE'))).select()

Massimo DiPierro

unread,
Jun 18, 2012, 12:17:43 AM6/18/12
to web2py-d...@googlegroups.com
In trunk!

-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/

Michael Toomim

unread,
Jun 18, 2012, 12:29:14 AM6/18/12
to web2py-d...@googlegroups.com
wheeee!
Reply all
Reply to author
Forward
0 new messages