Hi there,

First: I already tried to post this last week, but it doesn't seem like it 
worked. Apologies if this is a duplicate!

I stumbled upon a bit obscure behaviour using SQLFORM.grid, left joins and 
CSV exports using 2.8.1-stable+timestamp.2013.11.27.20.07.10. It seems to 
be closely related to #1584 here: 
http://code.google.com/p/web2py/issues/detail?id=1584

I made a simple test application which illustrates the problem here:

    https://bitbucket.org/fredreichbier/web2py-testexport

    (installable via the web2py admin interface using the git url 
g...@bitbucket.org:fredreichbier/web2py-testexport.git)

Similarly to the web2py manual, I have two tables, ``person`` (with a 
``person_name`` field) and ``thing`` (with a ``thing_name`` field and 
``owner_id``, a foreign key reference to a ``person`` entry). For testing, 
I added a person "Batman" with a thing "Batmobile" and a person "Spiderman" 
with a thing "Nothing".

I'm now constructing a SQLFORM.grid with a left join to make it possible to 
search for things by the owner's name:

    grid = SQLFORM.grid(db.thing,
                        fields=[db.thing.thing_name],
                        left=db.person.on(
                                db.person.id==db.thing.owner_id
                        ))

You can see the grid at http://127.0.0.1:8000/testexport/

If I'm now entering a query ``person.person_name contains "Batman"``, that 
is, filter the things by the owner's name, I get a list with one item, 
"Batman", which seems correct to me. However, if I now click "Export CSV", 
I get an empty file. If I'm filtering the things using the thing's name 
(``thing.thing_name contains "Bat"``), the grid displays one line, which 
can be correctly exported to CSV.

So, it seems to me that it's not possible to export a set of records when 
using a filter expression on fields of joined tables.

I looked into this a bit and I think it's related to the handling of 
keywords when using exports in SQLFORM.grid:

    
https://github.com/web2py/web2py/blob/a55c6777d39d238307445d937bc67dfd6a84d1b4/gluon/sqlhtml.py#L2173

``fields`` only contains the fields of ``thing``, thus, the query is 
rejected because ``person.person_name`` is unknown. However, for ordinary 
searching (not using exports), a ``sfields`` list of all searchable fields 
is constructed and used which also contains fields of joined tables (L2217):

    sfields = reduce(lambda a, b: a + b,
                 [[f for f in t if f.readable] for t in tables])

So my question is: Am I using the web2py API incorrectly here, or should 
SQLFORM.grid use a list similar to ``sfields`` also when handling exports?

Best Regards,

Friedrich


-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to