Hi,

i have just created a small patch to get virtual fields working with 
SQLFORM.grid.
Let me know when it doesn't work for you.
I have tested it only with my mysql db and without db options like left 
join, group, etc ..
A pull request is on the way.

Have fun using virtual field in you grid!


Am Dienstag, 18. Dezember 2012 08:03:05 UTC+1 schrieb software.ted:
>
> Does SQLFORM.grid support adding and display of a virtual field...am 
> getting an exception...my code:
>
> db.file_subject_issue.file_name = Field.Virtual(lambda row: 
> get_file_name(row.file_subject_issue.file_id))
> fields = [db.file_subject_issue.file_id, 
> db.file_subject_issue.date_created, db.file_subject_issue.file_name]
> query =....
>
> form = SQLFORM.grid(query,fields=fields,...)
>
> I am getting the error
>
> <type 'exceptions.AttributeError'> 'FieldVirtual' object has no attribute 
> '_tablename'
>
> 1.
> 2.
> 3.
> 4.
> 5.
> 6.
> 7.
> 8.
> 9.
> 10.
> 11.
> 12.
>
> Traceback (most recent call last):
>   File "/home/www-data/web2py/gluon/restricted.py", line 212, in restricted
>
>     exec ccode in environment
>   File 
> "/home/www-data/web2py/applications/intranet/controllers/administration.py" 
> <https://192.168.0.251/admin/default/edit/intranet/controllers/administration.py>,
>  line 176, in <module>
>
>   File "/home/www-data/web2py/gluon/globals.py", line 188, in <lambda>
>
>     self._caller = lambda f: f()
>
>   File 
> "/home/www-data/web2py/applications/intranet/controllers/administration.py" 
> <https://192.168.0.251/admin/default/edit/intranet/controllers/administration.py>,
>  line 71, in file_track
>
>     selectable=False,csv=False , paginate=20, user_signature=False)
>
>   File "/home/www-data/web2py/gluon/sqlhtml.py", line 1796, in grid
>
>     if field._tablename in tablenames]
> AttributeError: 'FieldVirtual' object has no attribute '_tablename'
>
>
>
>
> -- 
>
> .......................................................................................
> Teddy Lubasi Nyambe
> Opensource Zambia
> Lusaka, ZAMBIA
>
> Cell: +260 97 7760473
> website: http://www.opensource.org.zm
>
> ~/
> Human Knowledge belongs to the world! - AntiTrust
>
> Man is a tool-using animal. Without tools he is nothing, with tools he is 
> all - Thomas Carlyle 1795-1881
>
> /~
>  

-- 

--- 
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.


diff --exclude .git -u -r -N web2py_plain/gluon/dal.py web2py_patched/gluon/dal.py
--- web2py_plain/gluon/dal.py	2013-07-18 22:51:46.493777980 +0200
+++ web2py_patched/gluon/dal.py	2013-07-18 22:51:23.387066854 +0200
@@ -1538,6 +1538,7 @@
         args_get = attributes.get
         tablenames = tables(query)
         tablenames_for_common_filters = tablenames
+        fields = [f for f in fields if not isinstance(f,Field.Virtual)] #  skip virtual fields
         for field in fields:
             if isinstance(field, basestring) \
                     and REGEX_TABLE_DOT_FIELD.match(field):
@@ -2111,10 +2112,14 @@
         for tablename in virtualtables:
             ### new style virtual fields
             table = db[tablename]
+#            fields_virtual = [(f,v) for (f,v) in table.iteritems()
+#                              if isinstance(v,FieldVirtual)]
+#            print('fields_virtual(old): %s' %fields_virtual)
             fields_virtual = [(f,v) for (f,v) in table.iteritems()
-                              if isinstance(v,FieldVirtual)]
+                              if isinstance(v,FieldVirtual) and v.name in [f_.name for f_ in fields] and v.tablename == tablename]
             fields_lazy = [(f,v) for (f,v) in table.iteritems()
-                           if isinstance(v,FieldMethod)]
+                           if isinstance(v,FieldMethod) and v.name in [f_.name for f_ in fields] and v.tablename == tablename]
+#            print('fields_virtual(new): %s' %fields_virtual)
             if fields_virtual or fields_lazy:
                 for row in rowsobj.records:
                     box = row[tablename]
diff --exclude .git -u -r -N web2py_plain/gluon/sqlhtml.py web2py_patched/gluon/sqlhtml.py
--- web2py_plain/gluon/sqlhtml.py	2013-07-18 22:51:46.499777905 +0200
+++ web2py_patched/gluon/sqlhtml.py	2013-07-18 22:51:15.275168266 +0200
@@ -1928,6 +1928,7 @@
                     if isinstance(f,Field.Virtual) and f.readable:
                         f.tablename = table._tablename
                         columns.append(f)
+                        fields.append(f)
         if not field_id:
             field_id = tables[0]._id
         if not any(str(f)==str(field_id) for f in fields):
@@ -2239,7 +2240,10 @@
             limitby = None
 
         try:
-            table_fields = [f for f in fields if f.tablename in tablenames]
+            table_fields = list()
+            for f in fields:
+                if f.tablename in tablenames:
+                    table_fields.append(f)
             if dbset._db._adapter.dbengine=='google:datastore':
                 rows = dbset.select(left=left,orderby=orderby,
                                     groupby=groupby,limitby=limitby,
@@ -2247,6 +2251,7 @@
                                     cacheable=True,*table_fields)
                 next_cursor = dbset._db.get('_lastcursor', None)
             else:
+#                print('table_fields: %s' %([f_.name for f_ in table_fields],))
                 rows = dbset.select(left=left,orderby=orderby,
                                     groupby=groupby,limitby=limitby,
                                     cacheable=True,*table_fields)

Reply via email to