How do web2py request expressions work?

Recently I had the opportunity to take a look at the structure of web2py, and although I have some experience with Django and, moreover, with simple Python, I could not understand the meaning of the Query system that web2py uses.

Take this example from web2py book

db = DAL('sqlite://storage.db')
myquery = (db.mytable.myfield > 'A')
myset = db(myquery)
rows = myset.select()
for row in rows:
    print row.myfield

In a SO comment, the web2py author says that he (db.mytable.myfield > 'A')does not evaluate the True / False value directly and is actually evaluated for each row at the time of the selection. I understand that this allows you to use these expressions as query objects and even combine them.

I tried to find the answer to this online, but could not, so here is my question: how do these query expressions not evaluate True / False right away? Why is the value of myquery not, say, True? What Python function, which is probably missing, allows you to work?

+5
source share
3 answers

There are other answers, but just to provide more details on web2py:

db.mytable.myfield > 'A'

db.mytable.myfield web2py DAL Field, DAL Expression. Expression Python, ==, <, > .. Expression ( Field) DAL Query, Python. > (__gt__).

. Python.

+7

( ) . . , :

class DBField(...):
#...

   def __gt__(self,value):
      #building query object, based on value
      return query
+2

db.mytable.myfield is gluon.sql.SQLField that overrides the method __gt__, so an expression using the> operator evaluates gluon.sql.SQLQuery (see http://www.web2py.com/examples/static/epydoc /web2py.gluon.dal.Expression-class.html ).

+2
source

All Articles