如果需要,我想使用def clean(self)
引发验证错误。然而,在我可以使用它之前,我的所有字段都必须填充。我在views.py中设置了初始值,如下所示:
offering = DirectOffering.objects.get(id=offering_id)
current_user = InvestorProfile.objects.get(user=request.user)
form = DirectInvestmentForm(request.POST or None, initial={'offering': offering, 'user': current_user})
form.fields['user'].widget = forms.HiddenInput()
form.fields['offering'].widget = forms.HiddenInput()
if form.is_valid():
investment = form.save(commit=False)
investment.save()
但是,当我在错误页面上查看Local Vars时,我看到:
cleaned_data {'offering': None, 'quantity': 12345678, 'user': None}
数量是我输入的其他两个字段应该由initial = {}
自动填充