Atrotors
Atrotors

Reputation: 785

django "x time ago" outside templates

I know that in templates you can use naturaltime

{% load humanize %}
{{ video.pub_date|naturaltime }}

But is there anyway to implement this in python code?

class SomeObject(models.Model):
  req_tm = models.DateTimeField(auto_now_add=True)

    def __str__(self):
      return SomeFunction(self.req_tm)

Does SomeFunction exist?

Upvotes: 1

Views: 634

Answers (1)

Brandon Taylor
Brandon Taylor

Reputation: 34593

Template filters are just functions. All you need to do is import the function from the module and you can use it in any Python code.

from django.contrib.humanize.templatetags.humanize import naturaltime

class SomeObject(models.Model):
    req_tm = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return naturaltime(self.req_tm)

Upvotes: 3

Related Questions