There are views

res = SSwAlive.objects.filter(is_deleted=0) return render_to_response('test.html',{'res':res}) 

Data in res

  id: 38 sw: test ctime: 1317192076 utime: 1317194473 is_deleted: 0 

How to transfer the converted date to test.html? I understand that you can and so

 def dd(res): h = [] for j in for res: h.append("{0}".format(datetime.fromtimestamp(j[1]))) return h 

Can it somehow be easier?

    2 answers 2

    And why do not you want to write an additional method of type time_from_time_stamp to the model, which will return the normal datetime object (or what you need) from the timestamp?
    Type:

     MyModel(models.Model): field1 = .... def time_from_time_stamp(self) return datetime.fromtimestamp(self.utime) 

    Then both in the template and in the code you will access it as well as the other fields:

     ssw = SSwAlive.objects.get(is_deleted=0, ..., ...) some_var = ssw.time_from_time_stamp 
    • I want, only django did not do efficient projects, and it is necessary to rewrite. so to speak I consult. - avdoshkin
    • :) This is not Django in essence, just an ordinary Piton class method. The manager returns to you the usual instances of the class that you described as a Django model, so why not simply stick a method to them? :) - Andrey Basalyga
    • I have already stuck the method and everything turns out beautifully) - avdoshkin
    • (Trifle, but) some_var = ssw.time_from_time_stamp - it will not work that way. Or ssw.time_from_time_stamp() or ssw.time_from_time_stamp() @property before def . - drdaeman

    The most beautiful thing to do is not with the model method, but to realize your field . Then the conversion between UNIX timestamp and datetime will be transparent. See, for example, Using MYSQL's Unix Timestamp fields with Django

    • An interesting solution, thanks! Took note. - Andrey Basalyga