how can I make 'between' query with web2py.DAL?

Tags: python web2py
By : satoru
Source: Stackoverflow.com
Question!

I'm trying to make a query function that accepts two datetime.date object(start_date and end_date), and return all records with a related field that's between start_date and end_date. However, I found nothing like a between function in the web2py manual, so I implement it this way:

        for o in objects:
            # notice that create_time is a datetime field
            create_date = dt.datetime.strptime(o['create_time'], 
                                               "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")
        if query_dict['create_date_1'] <= create_date <= query_dict['create_date_2']:
            result.append(l)

Doing this is too slow for my application because their can be many objects. So, is there a better way I can implement this using web2py.DAL? Thanks in advance ;)

By : satoru


Answers

db((db.mytable.create_date>=query_dict['create_date1'])&(db.mytable.create_date<=query_dict['create_date2'])).select()

By : mdipierro


This video can help you solving your question :)
By: admin