0

模型.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

4

1 回答 1

1

is_big您已从模型表单中排除。这就是为什么它不存在于cleaned_data. 您需要从以下位置获取值POST

is_big = request.POST.get('is_big')
于 2013-01-23T18:47:04.550 回答