python - 使用类列表作为 django_tables2 中表类的模型

标签 python django django-tables2

我尝试使用与 django 中的数据库无关的类创建一个表,该类存储在 models.py 中,如下所示(InfoServer 是类)。我想做的是使用此类来使用 django_tables2 填充我的表。添加 models.Model 作为参数不是一个选项,因为我不想将此类保存在数据库中。

每当我在 tables.py 中定义 model = InfoServer 时,我都会收到此错误,我想这是因为 InfoServer 没有采用 models.Model 作为参数。

TypeError: descriptor 'repr' of 'object' object needs an argument

感谢任何帮助。

models.py

class TestServeur(models.Model):
    nom = models.CharField(max_length=200)
    pid = models.CharField(max_length=200)
    memoire = models.IntegerField(null=True)

class InfoServer:
    # "This is a class to test my knowledge of python"
    def __init__(self,p = '',c = 0,m = 0):
        self.pid = p
        self.cpu = c
        self.memoire = m

    def getData(self):
        return ("A server with %s memory and %s cpu" % (self.cpu,self.memoire))

views.py

def index(request):
    return HttpResponse("Hello, world. You're at the index.")

def cpu_view(request):
    liste = []
    proc1 = Popen(['ps','-eo','pid,%cpu,%mem,comm'], stdout=PIPE, stderr=PIPE)
    proc2 = Popen(['grep','java'], stdin=proc1.stdout, stdout=PIPE)
    proc1.stdout.close()

    for line in iter(proc2.stdout.readlines()):
        clean_line = line.decode("utf-8")
        info_utiles = clean_line.split()
        pid,cpu,mem,*rest = info_utiles
        i1 = InfoServer(pid,cpu,mem)
        liste.append(i1)

    table = TestServeur(liste)
    RequestConfig(request).configure(table)
    return render(request, 'server/cpu.html', {'output': table})

tables.py

class TableServeur(tables.Table):
    class Meta:
        # model = InfoServer
        fields = ['pid', 'memory', 'cpu']
        template_name = 'django_tables2/bootstrap4.html'

最佳答案

正如我所见,InfoServer 类不是 Django 模型。而且我认为你不需要直接使用它。因此,您可以简单地提供一个带有字典的列表,然后将其呈现在带有表格的模板中。

首先,我们需要更新 Table 类并从中删除 Meta 类,因为我们不会使用任何 django 模型。

class TableServeur(tables.Table):
    <b>pid = tables.Column()
    memory = tables.Column()
    cpu = tables.Column()</b>

现在,添加一个新的对象方法以从 InfoServer 类返回字典:

class InfoServer:
    # "This is a class to test my knowledge of python"
    def __init__(self,p = '',c = 0,m = 0):
        self.pid = p
        self.cpu = c
        self.memoire = m

    def getData(self):
        return ("A server with %s memory and %s cpu" % (self.cpu,self.memoire))

    <b>def get_dict_data(self):
        return {'pid': self.pid, 'cpu': self.cpu, 'memory': self.memoire}</b>

最后,更新 View :

for line in iter(proc2.stdout.readlines()):
    clean_line = line.decode("utf-8")
    info_utiles = clean_line.split()
    pid,cpu,mem,*rest = info_utiles
    i1 = InfoServer(pid,cpu,mem)
    liste.append(<b>i1.get_dict_data()</b>)
table = TestServeur(liste)
return render(request, 'server/cpu.html', {'output': table})

更多信息可以在documentation中找到关于如何用数据填充表。

关于python - 使用类列表作为 django_tables2 中表类的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56290443/

相关文章:

python - 如何在不旋转图像的情况下切换numpy中的图像表示

django开发服务器不工作

python - 我可以在 django-tables2 中制作二维表吗?

django - 如何清除 Django 模型中的一对一关系?

python - django-tables2 刷新排序和页面更改

javascript - 如何根据 ajax 请求动态更改 django_tables2 表

python - 如何从 python 中所有组合列表中的索引中知道元素的组合

python - 使用 python mechanize 和具有随机代理支持的 urllib2

python - pybind11 - ImportError : undefined symbol: _Py_ZeroStruct

django - 使用带有注释的 Django CheckConstraint