redefine default filtering behavior in Django templates


I have a project with many DecimalFields that are rendered in more than 300 templates. I would like that these decimal fields are rendered normalized. I don't care about precission or anything:


I haven't found a way to change default rendering system. I know there is a humanize and a floatformat filter I could use in my templates. But I need a solution that doesn't mean editing all those files, even if a shell script could be written.


By : maraujop

You could create a subclass of DecimalField where you override DecimalField.__str__ or DecimalField.__unicode__ to suit your needs. This method is called whenever the value needs to be rendered in a template or whatnot. You would only need to change your models.

The code for theField class is here. DecimalField is a subclass of this. Documentation about subclassing Field:

Also, see this tip abount the __unicode__ method:

Edit: I am waaay to tired to make any sense at the moment.

By : knutin

I finally came up with the answer to this question and posted it in my blog:

I hope somebody finds it useful

By : maraujop

This video can help you solving your question :)
By: admin