Searching by date using SQLFORM.factory

35 views
Skip to first unread message

mostwanted

unread,
Feb 19, 2020, 4:49:11 PM2/19/20
to web2py-users
I'm trying to use SQLFORM.factory to search by date but its failing, it is somehow possible to that & i'm missing something?? If so where can i rectify my code to give me results?

MY FUNCTION
def search():
    form
=SQLFORM.factory(Field('SEARCH', 'date')
   
if form.accepts(request):
        tokens
=form.vars.SEARCH.split()
        query
=reduce(lambda a,b:a&b, [db.birthdays.contains(k) for k in tokens])
        people
=db(query).select(orderby=db.birthdays.Person)
   
else:
        people
=[]
   
return locals()

Regards;

Mostwanted

Clemens

unread,
Feb 19, 2020, 5:05:15 PM2/19/20
to web2py-users
Hi,

is your problem receiving no result or the wrong result? How is the table definition of your birthday table and why is the birthday not just a field a corresponding person table?

Best regards
Clemens

mostwanted

unread,
Feb 19, 2020, 6:10:34 PM2/19/20
to web...@googlegroups.com
I figured it out, initially I was getting an error, it told me that I can not split the date, then i did this:

def search():
    form
= SQLFORM.factory(
   
Field("start_date", type="date"),
   
Field("end_date", type="date"))
   
if form.process().accepted:
        q
= ((db.birthdays.bornday >= form.vars.start_date) &(db.birthdays.birthday <= form.vars.end_date))
        rows
= db(q).select()
   
return locals()

& it worked, but now I am facing a-little challenge, I want to use the results of {{ rows }} in another function called clients, how can i do that?

Regards;

Mostwanted

mostwanted

unread,
Feb 19, 2020, 6:50:19 PM2/19/20
to web2py-users
I have decided to make rows a session variable but I am not sure if this is a good idea!

def search():
    form
= SQLFORM.factory(
   
Field("start_date", type="date"),
   
Field("end_date", type="date"))
   
if form.process().accepted:
        q
= ((db.birthdays.bornday >= form.vars.start_date) &(db.birthdays.birthday <= form.vars.end_date))

        session.rows
= db(q).select()
   
return locals()

I have a bad experience in the past with sessions when they start giving the NoneType error out of the blue!! Please please if someone has a better way of solving my problem please assist! What I want is to use the {{ rows }}  variable in another function like calling it in another function but I cant in web2py, i am getting an error, maybe i'm doing it wrong, show me how to do it.

Regards;

Mostwanted

Alex Beskopilny

unread,
Feb 20, 2020, 1:25:24 AM2/20/20
to web2py-users
import cPickle, os
def put_on_disk(file_name, data):

        uploadfolder=os.path.join(request.folder, 'uploads')
        fn = os.path.join(uploadfolder, file_name)
        out_file = open(fn, 'w')
        cPickle.dump(data, out_file)
        out_file.close()


def get_from_disk(file_name):

        uploadfolder=os.path.join(request.folder, 'uploads')
        fn = os.path.join(uploadfolder, file_name)
        in_file = open(fn, 'r')
        data = cPickle.load(in_file)
        in_file .close()
        return data
--------------------------------
save data on disk, add user.id to file name


четверг, 20 февраля 2020 г., 0:49:11 UTC+3 пользователь mostwanted написал:
Reply all
Reply to author
Forward
0 new messages