我有一個帶有單選按鈕和文本字段的表單。當我提交表單時,布爾型字段不會在記錄中創建。布爾字段應該通過單選按鈕進行更新。這裏可能是什麼問題?未保存在Django表格中的布爾字段
這裏是我的forms.py文件的相關部分:
CHOICES = (
(1,'yes'),
(0,'no')
)
class ServiceForm(forms.ModelForm):
one_time_service = forms.ChoiceField(required = True, choices = CHOICES, widget=forms.RadioSelect())
class Meta:
model = Service
fields = ('one_time_service')
這是我的models.py one_time_service場
one_time_service = models.BooleanField(default=False)
這是我的views.py:
def create(request):
if request.POST:
form= ServiceForm(request.POST)
if form.is_valid():
service_obj = form.save(commit=False)
service_obj.user_id = request.user.id
service_obj.save()
return render_to_response('services/service_created.html',
{'service': Service.objects.get(id=service_obj.id)})
else:
form = ServiceForm()
args= {}
args.update(csrf(request))
args['form'] = form
return render_to_response('services/create_service.html', args)
編輯:這是我的create_service.html
<form action="/services/create" method="post" enctype="multipart/form-data">{% csrf_token %}
<ul>
{{form.as_p}}
</ul>
<input type="submit" name="submit" value="Create Service">
</form>
您是否嘗試過將TRUE和FALSE而不是0和1? – Seether 2014-10-20 21:05:11
是的,我已經嘗試過。它不起作用 – 2014-10-20 21:07:32