python - ponyORM: trouble with query -


i have query dynamic conditions,i.e.

select (lambda obj:obj.a = 'a' , obj.b = 'b' , ...)   

so write code this:

def search(self,**kwargs):         q = unicode('lambda obj:', 'utf-8')     field,value in kwargs.iteritems():             value = unicode(value, 'utf-8')             field = unicode(field, 'utf-8')             q+=u" obj.%s == '%s' and" % (field,value      q = q[0:q.rfind('and')]      res = select(q.encode('utf-8'))[:] 

but have error during execution of function:

 tasks.search(title='Задача 1',url='test.com')  res = select(q.encode('utf-8'))[:]  file "<string>", line 2, in select  file ".../local/lib/python2.7/site-packages/pony/utils.py", line 96, in      cut_traceback return func(*args, **kwargs)   file ".../local/lib/python2.7/site-packages/pony/orm/core.py", line 3844, in select if not isinstance(tree, ast.genexpr): throw(typeerror)   file "...local/lib/python2.7/site-packages/pony/utils.py", line 123, in throw   raise exc   typeerror 

while possible use strings in order apply conditions query, can unsafe, because of risk of sql injection. better way applying conditions query using filter() method. can take latest version of pony orm https://github.com/ponyorm/pony repository , try couple of examples provided below.

first define entities , create couple of objects:

from decimal import decimal pony.orm import *  db = database('sqlite', ':memory:')  class product(db.entity):     name = required(unicode)     description = required(unicode)     price = required(decimal)     quantity = required(int, default=0)  db.generate_mapping(create_tables=true)  db_session:     product(name='ipad', description='air, 16gb', price=decimal('478.99'), quantity=10)     product(name='ipad', description='mini, 16gb', price=decimal('284.95'), quantity=15)     product(name='ipad', description='16gb', price=decimal('299.00'), quantity=10) 

now we'll apply filters passing them keyword arguments:

def find_by_kwargs(**kwargs):     q = select(p p in product)     q = q.filter(**kwargs)     return list(q)  db_session:     products = find_by_kwargs(name='ipad', quantity=10)     p in products:         print p.name, p.description, p.price, p.quantity 

another option use lambdas in order specify conditions:

def find_by_params(name=none, min_price=none, max_price=none):     q = select(p p in product)     if name not none:         q = q.filter(lambda p: p.name.startswith(name))     if min_price not none:         q = q.filter(lambda p: p.price >= min_price)     if max_price not none:         q = q.filter(lambda p: p.price <= max_price)     return list(q)  db_session:     products = find_by_params(name='ipad', max_price=400)     p in products:         print p.name, p.description, p.price, p.quantity 

as can see filters can applied dynamically. can find more information using filters following link: http://doc.ponyorm.com/queries.html#query.filter


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -