首页 > 解决方案 > 如何通过jinja2中另一个列表中的项目数重复列表项目

问题描述

我有 2 个列表、表格。我需要按列名的数量重复表名,但不包括第一个。

这是参数的一个例子

PARAMS = {'tables': ["table1", 
                     "table2"],
          'columns': ["col1", 
                      "col2", 
                      "col3"]
         }

这是我尝试过的语法:

QUERY_TEMPLATE = """
{% for table in tables %}
{{table | sqlsafe}}_{% for col in columns %}{% if not loop.first %}{{col | sqlsafe}}()
{% if not loop.last %},{% endif %}{% endif %}{%- endfor %}{%- endfor %}
"""

上面的语法给了我这个输出

table1_col2()
,col3()

table2_col2()
,col3()

但这是所需的输出

table1_col2(),
table1_col3(),

table2_col2(),
table2_col3(),

编辑:我正在使用一个名为jinjasql过滤| sqlsafe器的python库

标签: pythonloopsjinja2

解决方案


推荐阅读