1) Django的字段沒有被插件DJANGO,默認爲窗口小部件和DecimalField
爲IntegerField
是TextInput
。
而從this note您的聲明將覆蓋amount
字段的默認行爲。但是存儲的數據在Decimal
類型不int
所以如果你想代表它在int
你應該做的水木清華這樣的:
int(Decimal())
或閱讀Decimal docs
和設置自定義舍入。
2)在這個步驟中,您需要不便指定您的自定義渲染,所以有變化,你如何能做到這一點的數量(在這一刻我還記得他們兩個人):
2.1)簡單的一個 - 覆蓋的ModelForm INIT初始數據:如果您需要在花式小部件的定製表示你可以覆蓋)
class AmountInfoForm(forms.ModelForm):
class Meta:
model = Customer
fields = ('amount',)
def __init__(self, *args, **kwargs):
super(AmountInfoForm, self).__init__(*args, **kwargs)
amount_initial = self.initial.get('amount')
if amount_initial:
self.initial['amount'] = int(amount_initial)
2.2TextInput
(look at the source),並指定我t in your class Meta
docs:
class YourCustomWidget(TextInput):
def render(self, name, value, attrs=None):
if value is None:
value = ''
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
if value != '':
# Only add the 'value' attribute if a value is non-empty.
custom_value = int(value)
final_attrs['value'] = force_text(self._format_value(custom_value))
# here you can customise output html
return format_html('<input{0} />', flatatt(final_attrs))
class AmountInfoForm(forms.ModelForm):
class Meta:
model = Customer
fields = ('amount',)
widgets = {
'amount': YourCustomWidget(),
}
@madzohan你在說什麼解決方案是? – Atma 2014-11-05 19:01:40
忘了它,我是不留神)指定自定義的部件爲這個領域,並在該部件覆蓋渲染方法 – madzohan 2014-11-05 19:48:59
@madzohan你知道一個例子或它在文檔中的位置嗎?從幾次嘗試看來,整數字段沒有渲染方法。 – Atma 2014-11-05 21:42:07