首页 > 解决方案 > 在 django_tables2 中创建表时,有什么方法可以使用记录获取主键

问题描述

我试图通过使用 variables 来获取我的表的主键tables.py来使用 django-tables2 record.pk。我认为它record.pk最初存在,但不知何故它不存在。

class TableServeur(tables.Table):
    type_server = tables.Column()
    memory = tables.Column(attrs={"td": {"class": "memory"}}, verbose_name="% Mem")
    cpu = tables.Column(attrs={"td": {"class": "cpu"}}, verbose_name="% Cpu")
    port = tables.Column()
    etat = tables.Column()
    # here is the problem
    T1 = '<a class="btn" href="/update/{{record.pk}}" >Icon</a>'
    action = CustomTemplateColumn(T1)

    class Meta:
        fields = ["type_server", "cpu", "memory", "port", "etat", "action"]
        template_name = "django_tables2/bootstrap4.html"

视图.py

def cpu_view(request,nom_client):
    output = []
    liste = get_proc_output() 
    listServers = Serveurs.objects.all() # port, tp_serveur
    listNetstat = get_netstat_output() # port,pid,etat
    list_pcpu = get_proc_stat(liste) # pid,cpu,mem
    # we add percentage of cpu of each item
    for i in range(len(liste)):
        liste[i]['cpu'] = list_pcpu[i]
    # we mix and reorganise the list by their pid
    for x in liste:
        pid = x.get('pid')
        for y in listNetstat:
            if pid in y.values():
                output.append({**x,**y})    
    # we add the type of server by their port
    for i in listServers:
        for j in output:
            if i.port in j.values():
                j['type_server'] = i.type_du_serveur

    # if we have switched off server, print it with different way
    temp_list_1 = [i.type_du_serveur for i in listServers]
    temp_list_2 = [x['type_server'] for x in output]
    serveurs_eteint = list(set(temp_list_1) - set(temp_list_2))

    # 
    for j in serveurs_eteint:
        if j == k['type_server']:
            output.append({'type_server':j,'memory':'--','etat':'CLOSED'})

    for k in output:
        k['nom_client'] = nom_client
    # here we construct the table
    table = TableServeur(output)
    RequestConfig(request).configure(table)
    return render(request, 'server/table.html', {'table': table, 'nom': nom_client, 'output': output})

那么在使用 django-tbales2 创建表时,有什么方法可以直接获取主键?

标签: djangodjango-tables2

解决方案


推荐阅读