Pass a query from form to another function

95 views
Skip to first unread message

Áureo Dias Neto

unread,
Dec 7, 2016, 8:08:45 AM12/7/16
to web2py-users
I have a search form, which filter the fields, I filter my query .. so far so good ..

The search works perfectly, from the results I generate a PDF, but, how to pass this query from the search form to my PDF ..

Note that PDF is generated in another function.

Thank you!

António Ramos

unread,
Dec 7, 2016, 8:52:20 AM12/7/16
to web...@googlegroups.com
code please...

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Áureo Dias Neto

unread,
Dec 7, 2016, 8:57:22 AM12/7/16
to web...@googlegroups.com
The form consists in a query = db.veiculos.id>0

if user search filling any field, example 'name': 


---------
and on the end, i make the select

results = db(query).select()

This forms, work great, my question is, after the search is doing, user have the option to print an pdf, and this is a another function,
i want to pass this 'query' object to that function to generate the pdf doc.. because on pdf funcion im will make:

cars = db(query).select()

i want the same results on pdf function as well the form function!

Marlysson Silva

unread,
Dec 7, 2016, 9:33:16 AM12/7/16
to web2py-users
You can use sessions..

session.content = query_search 

and in your view responsible by generate pdf use:

generate_pdf(session.content)

Áureo Dias Neto

unread,
Dec 7, 2016, 11:04:03 AM12/7/16
to web...@googlegroups.com
with 'session.content = query_search' i save the query in session?

and request this on argument of function?

--

Anthony

unread,
Dec 7, 2016, 2:56:20 PM12/7/16
to web2py-users
On Wednesday, December 7, 2016 at 9:33:16 AM UTC-5, Marlysson Silva wrote:
You can use sessions..

session.content = query_search

A Query object can't be pickled to store it in the session, but you can first convert to a dictionary:

session.search_query = search_query.as_dict(flat=True)

Then in the second function:

db(session.search_query).select()

Anthony

Áureo Dias Neto

unread,
Dec 7, 2016, 9:03:33 PM12/7/16
to web...@googlegroups.com
Thank guys, it's work!!!!

--

Marlysson Silva

unread,
Dec 8, 2016, 12:29:23 PM12/8/16
to web2py-users
Can't by definition of DAL code? or by security..

Yoel Benitez Fonseca

unread,
Apr 10, 2018, 6:14:06 PM4/10/18
to web2py-users
Is this not working any more ?

>>> q = (db.photo.id == 1)
>>> db(q.as_dict(flat=True)).select()
 
File "<string>", line unknown
SyntaxError: Operator not supported: eq

That is in a web2py shell,i mean is the same thing passing around the query in the session as a dict, is not ?

Dave S

unread,
Apr 10, 2018, 9:27:03 PM4/10/18
to web2py-users


On Tuesday, April 10, 2018 at 3:14:06 PM UTC-7, Yoel Benitez Fonseca wrote:
Is this not working any more ?

>>> q = (db.photo.id == 1)
>>> db(q.as_dict(flat=True)).select()
 
File "<string>", line unknown
SyntaxError: Operator not supported: eq

That is in a web2py shell,i mean is the same thing passing around the query in the session as a dict, is not ?

 
The following works:

>>> q = (db.photo.id == 1)
>>> db(q).select().as_dict()

as_dict() and as_list() are defined as operating on a Rows object.  q is a query object.
<URL:http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#as_dict-and-as_list>

flat=True gives an unexpected keyword argument error.

.../pydal/base.py line 631 defines as_dict() for tables with flat and sanitize args, so you can do db(q).as_dict(flat=True) and get a query object dict (as it looks to me) but trying to put that back into db() fails.

(I tried with 2.15.4)
 
/dps

Anthony

unread,
Apr 10, 2018, 10:02:28 PM4/10/18
to web2py-users
On Tuesday, April 10, 2018 at 3:14:06 PM UTC-7, Yoel Benitez Fonseca wrote:
Is this not working any more ?

>>> q = (db.photo.id == 1)
>>> db(q.as_dict(flat=True)).select()
 
File "<string>", line unknown
SyntaxError: Operator not supported: eq

That is in a web2py shell,i mean is the same thing passing around the query in the session as a dict, is not ?

 
The following works:

>>> q = (db.photo.id == 1)
>>> db(q).select().as_dict()

as_dict() and as_list() are defined as operating on a Rows object.  q is a query object.
<URL:http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#as_dict-and-as_list>

flat=True gives an unexpected keyword argument error.

The Query class also has an as_dict method, though I don't think it is documented:


Anthony

Anthony

unread,
Apr 10, 2018, 10:20:04 PM4/10/18
to web2py-users
On Tuesday, April 10, 2018 at 6:14:06 PM UTC-4, Yoel Benitez Fonseca wrote:
Is this not working any more ?

>>> q = (db.photo.id == 1)
>>> db(q.as_dict(flat=True)).select()
 
File "<string>", line unknown
SyntaxError: Operator not supported: eq

That is in a web2py shell,i mean is the same thing passing around the query in the session as a dict, is not ?

Looks like it could be broken now. Feel free to file an issue with the PyDAL repository.

Anthony

Yoel Benitez Fonseca

unread,
Apr 20, 2018, 5:20:54 PM4/20/18
to web2py-users
Umm... sorry took long to answer, i have put on issue on PyDAL repo and a pull request with a posible fix:

Reply all
Reply to author
Forward
0 new messages