1

I'm trying to create a SQLFORM.grid in web2py that group by 2 fields and count them up, but I couldn't figure it out how to do it.

Model

db.define_table('invocados',
                Field('modulo_servico', 'string', label='Módulo (Serviço)', default=IS_LENGTH(2)),
                Field('servico', 'string', default=IS_LENGTH(8)),
           )

Controller

fields=(db.invocados.modulo_servico, db.invocados.servico)
Invocados=SQLFORM.grid(db.invocados.modulo_servico != db.invocados.modulo_consumidor, details=False, fields=fields, groupby=(db.invocados.modulo_servico | db.invocados.servico) , paginate=15)

I'm trying to count all records grouped by modulo_servico and servico. I've tried:

fields=(db.invocados.modulo_servico, db.invocados.servico, db.invocados.count())

but it doesn't work.

Can you help?

1
  • Sadly, there is only a few people that use web2py... Commented Oct 9, 2015 at 0:13

2 Answers 2

1

You could use a link in your grid to display the result:

SQLFORM.grid(db.invocados.modulo_servico != db.invocados.modulo_consumidor, 
    details=False, 
    fields=fields, 
    groupby=(db.invocados.modulo_servico | db.invocados.servico) , 
    paginate=15,
    links=[dict(header='Count', body=lambda row: row.modulo_servico + row.servico)])
Sign up to request clarification or add additional context in comments.

1 Comment

your answer was not 100% correct but I marked your answer as usefull because you pointed me to the right direction. I edit my previous answer and included your solution with the correction. thx
0

Until now I've been using web2py and I concluded that the best solution is to return the data in the controller and build a dedicated view, so that you can sort the results by Count field, like this:

Controller:

def servexpostos():
    q = db.invocados.modulo_servico != db.invocados.modulo_consumidor
    rows = db(q).select(db.invocados.modulo_servico, db.invocados.servico, db.invocados.servico.count(), groupby=db.invocados.modulo_servico | db.invocados.servico)
    return locals()

View:

<div class="col-md-12">
<table class="table table-striped table-hover">
    <thead>
        <tr>
            <th>Módulo</th>
            <th>Nome Implementação</th>
            <th># invocações de outros módulos</th>
        </tr>
    </thead>
    <tbody>
{{for i,row in enumerate(rows):}}
{{if i==items_per_page: break}}
        <tr>
            <td>{{=row.invocados.modulo_servico}}</td>
            <td>{{=row.invocados.servico}}</td>
            <td align="middle"><span class="badge">{{=row._extra['COUNT(invocados.servico)']}}</span></td>
        </tr>        
{{pass}}
    </tbody>
</table>

This solution have more more effort because I don't use SQLFORM.grid and I must control pagination, sorting, etc (common tasks of a web developer)...

A simpler aproach, but with no possibility of ordering the results by the "Count" field, is to use the links argument, of a SQLFORM.grid, and add a subquery in each row to count the records.

Example:

SQLFORM.grid(db.invocados.modulo_servico != db.invocados.modulo_consumidor, 
    details=False, 
    fields=fields, 
    groupby=(db.invocados.modulo_servico | db.invocados.servico) , 
    paginate=15,
    links=[dict(header='Count', body=lambda row: db(db.invocados.servico == row.servico).count())])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.