table的嵌套children的拼写方法
需要自定义返回json例子如下
{
"status":0,
"msg":null,
"data":{
"total":{{total}},
"rows":[
{% if rows %}
{% for item in rows %}
{% if loop.index0 > 0 %},{% endif %}
{
{% for field in item %}
{% if loop.index0 > 0 %},{% endif %}
"{{field}}":{{item[field]|tojson}}
{% endfor %}
,"children":[
{% for exline in "select * from `abc` where `tag2` = '%s'" | format(item['tag2']) | get_lines_from_sql %}
{% if loop.index0 > 0 %},{% endif %}
{
{% for exfield in exline %}
{% if loop.index0 > 0 %},{% endif %}
"{{exfield}}":{{exline[exfield]|tojson}}
{% endfor %}
}
{% endfor %}
]
}
{% endfor %}
{% endif %}
]
}
}
说明
{% for exline in "select * from `abc` where `tag2` = '%s'" | format(item['tag2']) | get_lines_from_sql %}
这句就是在做二次查询,用到了过滤器get_lines_from_sql
但由于sql里有where,所以要在 | 左边拼出sql的字符串,因此用了format过滤器
http://doc.yonyoucloud.com/doc/jinja2-docs-cn/templates.html#builtin-filters
问题没有解决?请留言提问
