> def search( self, **kw ): > by_where_clause = {} > for k,v in kw.items(): > by_where_clause[ k ] = v i guess u want to do query.filter_by(**by_where_clause) after that?
it's just a syntax sugar over .filter(). so by_where_clause = [] for k,v in kw.items(): col = getattr(self.c,k) if v == 'nn': # I use 'nn' to generate a NOT NULL by_where_clause.append( c <> None) else: by_where_clause.append( c == v) query.filter(and_(*by_where_clause)) that's 3 lines more .. svil On Thursday 19 March 2009 17:43:22 jo wrote: > Well, MIchael, in my case a notnull() function could be very > interesting because I'm using it in a function, and would like pass > values as parameters in such way: > > > def search( self, **kw ): > by_where_clause = [] > for k,v in kw.items(): > if k == 'myfield1': > if v == 'nn': # I use 'nn' to generate a NOT > NULL because we don't have a notnull() function > by_where_clause.append( self.c.field1 <> > None) else: > by_where_clause.append( self.c.field1 == > v) > > elif k == 'myfield2': > if v == 'nn': # I use 'nn' to generate a NOT > NULL because we don't have a notnull() function > by_where_clause.append( self.c.field2 <> > None) else: > by_where_clause.append( self.c.field2 == > v) > > elif k == 'myfield3': > if v == 'nn': # I use 'nn' to generate a NOT > NULL because we don't have a notnull() function > by_where_clause.append( self.c.field3 <> > None) else: > by_where_clause.append( self.c.field3 == > v) ... > > > > > Mytb.search(myfield=None) -- generates WHERE myfield IS NULL > Mytb.search(myfield=null()) -- generates WHERE myfield IS NULL > Mytb.search(myfield='nn') -- generates WHERE myfield IS NOT NULL > > > > if we have a notnull() function these thing could be easier: > > def search( self, **kw ): > by_where_clause = {} > for k,v in kw.items(): > by_where_clause[ k ] = v > > > Mytb.search(myfield=None) -- generates WHERE myfield IS NULL > Mytb.search(myfield=null()) -- generates WHERE myfield IS NULL > Mytb.search(myfield=notnull()) -- generates WHERE myfield IS NOT > NULL > > Michael Bayer wrote: > > well usually null() and not_(null()) aren't needed as explicit > > constructs. comparisons like somecol == None and somecol != > > None will generate the appropriate NULL/NOT NULL expression. > > > > On Mar 19, 2009, at 4:48 AM, jo wrote: > >> Hi all, > >> > >> I would like to know if there's a notnull() function in > >> sqlalchemy similar to null() > >> to avoid things like not_(null()) ? > >> > >> thank you > >> > >> j > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---