[英]Python 3 / Django 2, Passing a dictionary value to form field before saving
I'm trying to get my app to use a serial number, entered on a form, to compare to a dictionary and update a product field on a form before saving. 我正在尝试让我的应用使用在表单上输入的序列号来与字典进行比较,并在保存之前更新表单上的产品字段。 Here's the details: 详细信息如下:
Here is my view: 这是我的看法:
def create(request):
#Page with input form to submit new records
if request.method == 'POST':
form = RecordForm(request.POST)
if form.is_valid():
sn = request.POST.get('serial')
with open('prodlist.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if sn[0:2] in row['Product Code']:
form.fields['product'].update = row['Product']
else:
pass
form.save()
return HttpResponseRedirect('/tracker/all/')
else:
form = RecordForm()
return render(request, 'tracker/form2.html', {'form': form}, RequestContext(request))
With this, I'm geting a key error for the 'product' piece. 这样,我在“产品”部分遇到了一个关键错误。 Is there a way to pass a value to a form field after it is submitted? 提交值后,是否可以将值传递给表单字段?
Update: 更新:
I've added the product field to the form itself in forms.py: 我已经在form.py中将产品字段添加到表单本身:
class RecordForm(ModelForm):
class Meta:
model = Record
fields = ['serial',
'ticket',
'product',
'eng_date',
'customer',
'details',
'owner',
'status',
'seed_num'
]
widgets = {
'eng_date': DateInput(),
}
And I've changed the form.fields['product'].update = row['Product']
line to form.cleaned_data['product'] = row['Product']
. 而且我已经将form.fields['product'].update = row['Product']
行更改为form.cleaned_data['product'] = row['Product']
。 and added an else: clause to write something anyway, in case it doesn't find the serial: else: form.cleaned_data['product'] = '5678'
并添加了else:子句以编写任何内容,以防万一找不到序列号: else: form.cleaned_data['product'] = '5678'
Here's the full view: 这是完整视图:
def create(request):
#Page with input form to submit new records
if request.method == 'POST':
form = RecordForm(request.POST)
if form.is_valid():
sn = request.POST.get('serial')
with open('prodlist.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if sn[0:2] in row['Product Code']:
form.cleaned_data['product'] = row['Product']
else:
form.cleaned_data['product'] = '5678'
form.save()
return HttpResponseRedirect('/tracker/all/')
else:
form = RecordForm()
return render(request, 'tracker/form2.html', {'form': form}, RequestContext(request))
Any help would be greatly appreciated :) 任何帮助将不胜感激 :)
试试这个
form.cleaned_data['product'] = row['Product']
I was able to get it working! 我能够使它正常工作! Had to use obj = form.save(commit=false)
, then pass the value to that as obj.product
, and then ust obj.save()
. 必须使用obj = form.save(commit=false)
,然后将值传递给obj.product
,然后再传递obj.save()
。 Here is the view as it stands: 实际情况如下:
def create(request):
#Page with input form to submit new records
if request.method == 'POST':
form = RecordForm(request.POST)
if form.is_valid():
sn = request.POST.get('serial')
with open('prodlist.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if sn[0:2] in row['Product Code']:
obj = form.save(commit=False)
obj.product = row['Product']
obj.save()
break
else:
pass
form.save()
return HttpResponseRedirect('/tracker/all/')
else:
form = RecordForm()
return render(request, 'tracker/form2.html', {'form': form}, RequestContext(request))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.