single_record = db(query).select().first() if not single_record: print "no record" else: print "record!", single_record
You can also do db(query).count() db(query).select().last() -Thadeus On Tue, Mar 16, 2010 at 3:45 PM, Michael Toomim <too...@gmail.com> wrote: > Hi guys, I've found the following functions to be commonly useful in > practice. Has anyone else written anything similar? Is there a better > idiom here, or better names or interfaces for these? > > def get_one(query): > result = db(query).select() > assert len(result) <= 1, "GAH get_one called when there's MORE > than one!" > return result[0] if len(result) == 1 else None > > > def get_or_make_one(query, table, default_values): > result = get_one(query) > if not result: > table.insert(**default_values) > return get_one(query) > > -- > You received this message because you are subscribed to the Google Groups > "web2py-users" group. > To post to this group, send email to web...@googlegroups.com. > To unsubscribe from this group, send email to > web2py+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/web2py?hl=en. > > -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.