开发者

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 thes开发者_C百科e 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜