我有一个交易模型。该交易模型有一个金额字段(DecimaField)。当我尝试输入1,000时,它不允许我保存数据但没有显示错误,它只是刷新。我尝试将本地化放在模型表单中,但它仍然不允许我。
如何让表单接受逗号(1,000)?
这是我的代码:
表格.py
class AddTransactionForm(forms.ModelForm):
amount = forms.DecimalField(localize=True)
class Meta:
model = Transaction
exclude = ('envelope', 'uid',)
def __init__(self, user, *args, **kwargs):
super(AddTransactionForm, self).__init__(*args, **kwargs)
//other codes here
视图.py
在完全保存之前,我也尝试在视图中替换逗号,但它给了我一个错误。
def transaction(request):
if request.method == 'POST':
if request.POST['process'] == 'addtrans':
tform = AddTransactionForm(request.user,
request.POST)
if tform.is_valid():
if request.POST['type'] == 'withdrawal':
trans = tform.save(commit=False)
trans.amount = 0 - trans.amount
trans.save()
messages.success(request,
"Transaction created successfully.")
else:
trans = tform.save()
messages.success(request,
"Transaction created successfully.")
if not trans.account:
env = Envelope.objects.get(
user=request.user,
name='Unallocated Income')
trans.envelope = env
trans.save()
env.amount += trans.amount
env.save()
messages.success(request,
"Unallocate income save")
return HttpResponseRedirect(reverse('transactions:transaction'))
return render(request, 'trans_list.html', {
.........
})
我不想修改我的模型。表单或视图中有方法吗?