如何使用django-tables2从自定义sql呈现表? [英] How to render table from custom sql with django-tables2?

查看:691
本文介绍了如何使用django-tables2从自定义sql呈现表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Django和django-tables2一起工作,以便在Web界面中对sql查询进行很好的表示。我有一个遗留的sql代码,通过标准的models.py来定义它非常复杂。



问题是:如何从自定义sql查询中呈现一个表使用django-tables2?

解决方案

填充数据表显示如何创建一个具有字典列表作为输入数据的表。

 导入django_tables2作为表

data = [
{name:Bradley},
{name:Stevie},
]

class NameTable(tables.Table):
name = tables.Column()

table = NameTable(data)

假设您的自定义sql查询以类似格式返回数据,您应该能够使用相同的方法。


I'm working with Django and django-tables2 to make a nice representation of sql queries in a web-interface. I have a legacy sql code which is very-very complicated to define it throught standard models.py.

The question is: how can i render a table from custom sql query using django-tables2?

解决方案

The docs on populating a table with data show how you can create a table with a list of dictionaries as the input data.

import django_tables2 as tables

data = [
    {"name": "Bradley"},
    {"name": "Stevie"},
]

class NameTable(tables.Table):
    name = tables.Column()

table = NameTable(data)

Assuming your custom sql query returns data in a similar format, you should be able to use the same approach.

这篇关于如何使用django-tables2从自定义sql呈现表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆