我有一个客户的请求,要有页面管理字段,他们可以用逗号(例如1,000,000)添加/读取数字。
用于存储值的Django模型字段将是django.db.models.fields.DecimalField
实例。
通过查看Django文档,这是django.forms.fields.DecimalField
localized
属性支持的内容,但是即使将Wagtail BaseFieldPanel
__init__
函数子类化,我也找不到在Wagtail admin中强制实施该方法的方法。与self.bound_field.field.localize = True
。
最佳答案
您可以通过自定义此处记录的生成表单来覆盖Wagtail FieldPanel使用的字段:
http://docs.wagtail.io/en/v1.13/advanced_topics/customisation/page_editing_interface.html#wagtail.wagtailadmin.forms.WagtailAdminPageForm
下面的基本示例-myapp/models.py
from django import forms
from django.db import models
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtail.wagtailadmin.forms import WagtailAdminPageForm
from wagtail.wagtailcore.models import Page
class MyCustomPageForm(WagtailAdminPageForm):
# fields here are django.forms fields
# when set to localize, renders as a TextInput widget
total_amount = forms.DecimalField(localize=True)
# when left with defaults, renders as a NumberInput widget
# total_amount = forms.DecimalField()
# can also set any kind of widget here
# total_amount = forms.DecimalField(widget=MyCustomDecimalWidget)
class MyCustomPage(Page):
# fields here are django.db.models fields
total_amount = models.DecimalField()
content_panels = Page.content_panels + [
FieldPanel('total_amount'),
]
base_form_class = MyCustomPageForm # important: must set this
当您将表单字段设置为
localize=True
时,它似乎遵循Django Docs的预期行为,即呈现TextInput小部件。如果要执行一些更复杂的功能,也可以为页面的表单类中的任何字段设置自己的小部件。
关于python - 在Wagtail页面admin中对DecimalField使用逗号格式的数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47334766/