cat models.py from django.db import models from datetime import datetime def GetInHMS(seconds): hours = seconds / 3600 seconds -= 3600*hours minutes = seconds / 60 seconds -= 60*minutes return "%02d:%02d:%02d" % (hours, minutes, seconds) class SSwAlive(models.Model): id = models.IntegerField(primary_key=True) sw = models.CharField(max_length=255, blank=True) ctime = models.IntegerField(null=True, blank=True) utime = models.IntegerField(null=True, blank=True) is_deleted = models.IntegerField(null=True, blank=True) def __unicode__(self): return "{0} {1} {2}".format(GetInHMS(self.utime-self.ctime),datetime.fromtimestamp(self.ctime),self.sw) class Meta: db_table = u's_sw_alive' from django.shortcuts import render_to_response from soffice.app_ping.models import * cat views.py def ping(request): res = SSwAlive.objects.filter(is_deleted=0) return render_to_response('switch.html',{'res':res}) cat switch.html {%for uu in res%} {{uu}} {%endfor%} 

The question is how do I get the uu variable columns separately to create a normal table. If uu.sw is done, this column is appropriate, and all the others that I return from models.py how to get them? I need to separately receive:

GetInHMS (self.utime-self.ctime), datetime.fromromstastamp (self.ctime)

    1 answer 1

    If I understand the question correctly, do you want to perform calculations by a function for each object and output the result to a template? If so, then consider all this data in the view function and create a list of tuples, something like this:

     ([res[0], GetInHMS(res[0].utime-res[0].ctime), datetime.fromtimestamp(res[0].ctime)], [...], ...) 

    So we obtain a data structure consisting of a sequence of 3 elements: the SSwAlive itself and 2x of the unknown values. I think that it will not be difficult to expand the list in the template.

    • Everything is calculated. The __unicode__() method returns a date, and I need to break the string in a template, such as: {% for u in% res}} {{uu.0}} {{uu.1}}, etc. {% endfor%} - avdoshkin
    • Make a property in the class, a la @property def as_tuple (self): return (GetInHMS (self.utime-self.ctime), datetime.fromtimestamp (self.ctime), self.sw) And in the template, respectively {{ uu.as_tuple.1 }} . Or will not go? - drdaeman
    • something seems to be true, now check. - avdoshkin
    • Thanks, all worked well! - avdoshkin 4:02 pm
    • @avdoshkin, do not forget to accept the answer. - angry