Issue #52 wontfix

ObjectGrid doesn't handle multi-level class attributes...

Rod Morison
created an issue

...e.g., those generated on App Engine from relations:

{{{ class FirstModel(db.Model): prop = db.IntegerProperty()

class SecondModel(db.Model): reference = db.ReferenceProperty(FirstModel)

ObjectGrid(SecondModel.all().fetch(10), [ 'reference.prop' ]) }}}

I propose the following mode to ObjectGrid class:

{{{

class ObjectGrid(Grid): """ A grid class for a sequence of objects.

This grid class assumes that the rows are objects rather than dicts, and
uses attribute access to retrieve the column values. It works well with
SQLAlchemy ORM instances.
"""
def default_column_format(self, column_number, i, record, column_name):
    class_name = "c%s" % (column_number)
    attr_val = record
    for attr_name in column_name.split('.'):
        attr_val = getattr(attr_val, attr_name)
    return HTML.tag("td", attr_val, class_=class_name)

}}}

Comments (2)

  1. Marcin Lulek

    The grid is completly agnostic in regards to what datasource you want to use with it.

    I suppose this is where you want to use custom column format functionality, ie. you can define custom rendering function for any specific column you want to use.

    Also if you need to change the way data is fetched in every column for your grid - just subclass one of Grid classes and overload the default_column_format if that works for you, you will notice that this is actually what ObjectGrid does basicly, as i designed the class to allow this kinds of tricks to be easy.

    Also, what if you want to fetch [ 'reference.prop.bar.baz' ]) ? because its something that you may want to do with complicated nested model - structures.

    I think your best friend is the custom format functionality:

    def custem_part_no_td(col_num, i, item):
        return HTML.td(item.foo.bar)
    
    grid.column_formats["part_no"] = custem_part_no_td
    

    You will want to use that to generate url-s or format dates, etc.

  2. Log in to comment