How to display human time (x days ago, or now) in django admin?
There is an accepted answer however I want to suggest another thing who came here for a solution which can be used in templates.
Django has a contrib package called django.contrib.humanize
. Add this to your INSTALLED_APPS, then use {% load humanize %}
in your template, after that, you can use value|naturaltime
template tag. "value" will be your date.
Let's say content.created
, which contains the creation date of your content. It's a datetimefield object. So, you can use this: content.created|naturaltime
. It'll convert the date e.g from July 14, 2018 11:30 to "3 days 1 hour ago".
Check it on Django DOCS.
Turns out the template filters can be accessed directly from your Python codebase. Something like this:
from django.contrib.humanize.templatetags import humanize
class Example(models.Model):
date = models.DateTimeField(auto_now_add=True)
def get_date(self):
return humanize.naturaltime(self.date)
The get_date()
function can also be located within your CourseAdmin
class.