如何本地化 Django admin 中的 list_display 字段?
我在 ModelAdmin 类的 list_display 中有浮动字段,我希望它们本地化显示(如 1.000,00)。我已经将 USE_L10N 和 USE_I18N 设置为 True,但这还不够。
我当前的解决方案如下:
from django.utils import formats
class MyAdmin(admin.ModelAdmin):
list_display = ('localized_myfield')
def localized_myfield(self, instance):
return formats.localize(instance.myfield)
可行,但是为每个浮点字段定义一种方法看起来像很多不必要的代码。有没有更简单的方法来做到这一点?
I have float fields in the list_display of an ModelAdmin class, and I'd like them to be displayed localized (like 1.000,00). I already have USE_L10N and USE_I18N to True, but that's not enough.
The current solution I have is the following:
from django.utils import formats
class MyAdmin(admin.ModelAdmin):
list_display = ('localized_myfield')
def localized_myfield(self, instance):
return formats.localize(instance.myfield)
That works, but defining one method for each float field I have looks like a lot of unnecessary code. Is there a simpler way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番研究后我找到了答案。为了实现我想要的,需要三件事:
I found the answer after some research. To achieve what I want, three things are necessary: