简体   繁体   中英

Using a list of classes as a model for table class in django_tables2

I tried to create a table using a class that is not related to my database in django and this class is stored in models.py as shown below ( InfoServer is the class). What I wanted to do is to use this class to populate my table using django_tables2 . Add models.Model as a parameter is not an option because I don't want to save this class in the database.

Whenever I define the model = InfoServer in tables.py I got this error and I suppose it's because InfoServer did not take models.Model as a parameter.

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

Any help is appreciated.

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'

As I can see, InfoServer class is not a Django Model. Also I don't think you need to use that directly anyway. So, you can simply provide a list with dictionary, and render it in template with table.

First, we need to update Table class and remove Meta class from it, as we are not going to use any django models.

class TableServeur(tables.Table):
    

Now, adding a new object method to return dictionary from InfoServer class:

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))

    

Finally, update the 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()
table = TestServeur(liste)
return render(request, 'server/cpu.html', {'output': table})

More info can be found in documentation on how you can populate table with data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM