Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use django.contrib.humanize in a model

I would like to use the django.contrib.humanize outside of a template, actually inside a model to humanize some dates in some text messages.

Is it possible? How can I do this?

like image 218
André Luiz Avatar asked Jun 22 '16 13:06

André Luiz


People also ask

What is Django contrib humanize?

humanize. A set of Django template filters useful for adding a “human touch” to data.

What does contrib mean in Django?

Contrib name is for software that has been contributed to the project, but which might not actually be maintained by the core developers. Naming it "contrib" or "Contrib" is a long-established convention, but there's really nothing special about the name, and it's usually only used by fairly large projects.

Which package contains filters that can be used to translate dates into?

Django comes with a set of template filters to add a “human touch” to your data. It is used to translate numbers and dates into a human readable format.


1 Answers

You can import the functions in the model:

from django.contrib.humanize.templatetags.humanize import naturalday
natural_day = naturalday(value)
like image 200
André Luiz Avatar answered Oct 13 '22 07:10

André Luiz