模型.py:
class Data(models.Model):
name = models.CharField(max_length=250)
is_big = models.BooleanField(default=False)
表格.py:
class DataForm(ModelForm):
class Meta:
model = Data
exclude = ['is_big']
而不是is_big
从我创建的模型表单(在模板中):
<div class="id_is_big-control-group control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" disabled="disabled" checked="checked" name="is_big" id="id_is_big" /> <span>Is big?</span>
</label>
</div>
如何将此值保存在数据库中?
form = DataForm(request.POST)
if form.is_valid():
d = form.save(commit=False)
d.is_big = form.cleaned_data['is_big']
d.save()
返回错误:/site/'is_big' 处的 KeyError