Datagrid w/o SA

44 views
Skip to first unread message

Db

unread,
May 12, 2012, 2:44:34 AM5/12/12
to turbo...@googlegroups.com

Is it possible  to load a Datagrid without sqlalchemy help ?

Current testing code snippets:

from tw.forms import DataGrid

contactinfo_grid = DataGrid(fields=[
    ('name','name'),
    ('surname','surname'),
    ('phone','phone')
    ])


and this code inside the controller:


 @expose('p1.templates.index')
    def index(self):
      data=[{'phone': '123456', 'surname': 'Sample', 'name': 'Frank'}]
      return dict(page='index', grid=contactinfo_grid, data=data)


in the index.html:

<div>${grid(data)}</div>

 
Obviously the data is not in the correct format, but what should it look like?

The idea is to just reuse existing, well tested sql queries from a pyqt application ,
which we intend to port to tg2 , without going thru sqlalchemy magic.


Thanks for any pointers


Db











Simone Marzola

unread,
May 12, 2012, 7:18:06 AM5/12/12
to turbo...@googlegroups.com
Hi,
if you define the getter of the grid column as a string, it will be used to return the value of an object attribute (getattr(obj, name)). If you want to define a custom accessor, the getter must be a callable that defines the column behavior. In specific:

 contactinfo_grid = DataGrid(fields=[
    ('name', lambda row: row.get('name')),
    ('surname', lambda row: row.get('surname')),
    ('phone', lambda row: row.get('phone'))
    ])

Alessandro Molina

unread,
May 12, 2012, 2:43:16 PM5/12/12
to turbo...@googlegroups.com
Yes it is possible, but Datagrid expects values to be objects with
attributes instead of a dict.
To workaround this issue you can store the values (or convert the dict
to) a tg.util.Bunch object which will provide access both with
__getitem__ and __getattr__.

In any case DataGrid permits to pass any callable to retrieve the
column values when declaring the fields attribute instead of property
name.

Give a look at the DataGrid tutorial for more informations:
http://www.turbogears.org/2.1/docs/main/DataGrid/index.html
> --
> You received this message because you are subscribed to the Google Groups
> "TurboGears" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/turbogears/-/A57pZW-XqFkJ.
> To post to this group, send email to turbo...@googlegroups.com.
> To unsubscribe from this group, send email to
> turbogears+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/turbogears?hl=en.

Db

unread,
May 28, 2012, 3:37:40 AM5/28/12
to turbo...@googlegroups.com
 
Thank you both, it works nicely.

   Db   
Reply all
Reply to author
Forward
0 new messages