【问题标题】:Splitting the count value from my group by, sqlalchemy flask通过 sqlalchemy 烧瓶从我的组中拆分计数值
【发布时间】:2021-05-31 03:13:52
【问题描述】:

所以这是我的问题,我正在尝试显示一个表格,该表格基本上显示了特定型号汽车的销售量。像这个,

不幸的是,我不知道如何将计算的数量与模型分开。是否有任何回调或拆分函数可以帮助我解决这个问题?

这是html模板的代码

<div class="table-responsive-lg mt-4 px-4 pt-4 p-0" style="font-size: 14px;">
<table class="table table-dark text-center mx-auto my-0 " style="width: 50%; opacity: .9; padding-top: 50px;">
        <div class="bg-dark mx-auto px-1 mb-0 text-center" style="width: 769px; opacity: .9;">
        <p class="m-0">Tabela dos modelos mais vendidos</p>
        </div>
<thead>
    <tr>
        {% for cabeca2 in TituloTop20Modelo %}
            <th scope= 'col'> {{ cabeca2 }}</th>
        {% endfor %}
    </tr>
</thead>
<tbody>
    <tr>
    {% for numero in ContadorGrupo %}
    <td>
        {{numero.ModeloDB}}
    </td>
    <td>
        {{numero}}
    </td>
    </tr>
    {% endfor %}        
</tbody>
</table>

这是我用来获取变量的特定查询。

return render_template("TerceiraJanela.html",TituloTop20Modelo= TituloTop20Modelo, ContadorGrupo= Dado.query.with_entities(Dado.ModeloDB, func.count(Dado.ModeloDB)).group_by(Dado.ModeloDB).all())

【问题讨论】:

    标签: python html flask flask-sqlalchemy


    【解决方案1】:

    我倾向于标记我的列以使这更容易:

    你有:

    Dado.query.with_entities(Dado.ModeloDB, func.count(Dado.ModeloDB)).group_by(Dado.ModeloDB).all())

    将其更改为(使用label):

    Dado.query.with_entities(Dado.ModeloDB, func.count(Dado.ModeloDB).label('sold')).group_by(Dado.ModeloDB).all())

    所以现在在你的模板中可以使用:

    numero.ModeloDBnumero.sold 引用这些值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 2021-01-24
      • 2022-01-14
      • 2016-11-14
      • 2020-02-19
      • 2014-07-20
      相关资源
      最近更新 更多