1๐
โ
I would expose a property on the model that does what you require. Template tags are not really designed to be used outside of templates (though of course they can as they are just functions after all), and furthermore using them means your model code depends on view specific code, which is not ideal (typically view code will depend on model code, not the other way round).
@property
def my_date_formatted(self):
return self.my_date.strftime('%B %Y')
Note that when using strftime
instead of the template tag, the syntax for specifying the date format is different, there is a good reference at http://strftime.org/.
๐คrobjohncox
1๐
You can call this filter right in your python code:
from django.template.defaultfilters import date as date_filter
date = date_filter(object.my_date, "F Y")
๐คcatavaran
- [Answered ]-Second Django Logout Does Not Work
- [Answered ]-Updating model objects in the database on Django
Source:stackexchange.com