在forms.py中,我创建了一个application_status小部件并将其添加到customerForm中。如下:
APPLICATION_STATUS = [
('new', 'New'),
('processing', 'Processing'),
('reject', 'Rejected'),
('complete', 'Completed'),
]
class CustomerForm(forms.ModelForm):
application_status = forms.CharField(label='What is your application status?',
widget=forms.Select(choices=APPLICATION_STATUS))
# email_one = forms.EmailField()
class Meta:
model = Customer
fields = ('application_status')
然后在view.py中,我将选定的值保存在数据库中,但是它保存了选定选项的名称而不显示值。例如,我选择“处理”选项,并且希望将“处理”值保存在数据库中。但我看到“处理”已保存。代码如下:
def customer_new(request):
if request.method == "POST":
form = CustomerForm(request.POST)
if form.is_valid():
customer = form.save(commit=False)
customer.status = request.POST.get('application_status')
customer.save()
return redirect('customer_detail', pk=customer.pk)
else:
form = CustomerForm()
return render(request, 'customer_edit.html', {'form': form})
请帮忙一点。
答案 0 :(得分:0)
我认为现在的做法是正确的。它应使用APPLICATION_STATUS
的第一个实例存储在数据库中,第二个作为显示版本。如果您需要存储元组中的第二个值,则更改APPLICATION_STATUS
以反映出来:
APPLICATION_STATUS = [
('New', 'New'),
('Processing', 'Processing'),
('Rejected', 'Rejected'),
('Completed', 'Completed'),
]
但再次,我认为那是错误的选择。
答案 1 :(得分:0)
正确的答案是:
opciones = (
('a','A'),
('a1','A1')
)
class Producto(ModelForm):
a = forms.ChoiceField(choices=opciones)
...这样,框架将向您显示这些选项的选择...