web2py / py4web

Other
244 stars 126 forks source link

grid refactoring #893

Closed mdipierro closed 1 month ago

mdipierro commented 2 months ago

This now works for example:

db.define_table(
    'thing',
    Field('name', requires=IS_NOT_EMPTY()),
    Field.Virtual('name2', lambda row: row["name"]),
    Field('in_stock', 'boolean', default=True),
    Field('f_date', 'date', default=datetime.date.today()),
    Field('f_datetime', 'datetime', default=datetime.datetime.now()),
    Field('f_time', 'time', default="12:30"),
    Field('properties', 'list:string'),
    Field('image', 'upload', download_url=lambda name: URL('download', name)),
)

db.define_table(
    'part',
    Field('thing_id', "reference thing"),
    Field('name', requires=IS_NOT_EMPTY()),
    Field('weight', "float"),
)

@action('index')
@action('index/{path:path}')
@action.uses('generic.html', db)
def _(path=None):
    query = db.thing.id > 0
    weight = db.part.weight.sum()
    left = db.part.on(db.thing.id==db.part.thing_id)
    grid = Grid(path, query=query, left=left, groupby=db.thing.id, 
                columns=[
                    db.thing.name,
                    db.thing.name2,
                    db.thing.in_stock,  # shows as checkbox, was broken, use unicode now
                    db.thing.f_date,
                    db.thing.f_datetime,
                    db.thing.f_time,
                    db.thing.properties,
                    weight, # did not work before
                    Column("weight", lambda row: row[weight], required_fields=[weight]) # same as above
                ])
    return locals()
mdipierro commented 1 month ago

Got no feedback except on mailing list. Approving my own PR. :-(