redefine default filtering behavior in Django templates

Question!

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:

decimal.Decimal("10.0000").normalize()

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.

Thanks

By : maraujop


Answers
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: http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#writing-a-field-subclass

Also, see this tip abount the __unicode__ method: http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#some-general-advice

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: http://tothinkornottothink.com/post/2156476872/django-positivenormalizeddecimalfield

I hope somebody finds it useful

By : maraujop


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