satoru
satoru

Reputation: 33225

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

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 ;)

Upvotes: 3

Views: 2085

Answers (1)

mdipierro
mdipierro

Reputation: 4248

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

Upvotes: 3

Related Questions