我有一个formset,我想检查每个表单以确保我的阅读对象不大于我的目标对象。出于某种原因,我无法使其正常工作,因为如果读数大于它仍然保存的目标,那么我的表单错误也不会显示出来。如何检查每个表单以验证每个读取对象是否小于我的目标对象任何帮助将不胜感激。
以下是views.py
中的formset验证class BaseInspecitonFormSet(BaseInlineFormSet):
def clean(self):
if any(self.errors):
return
reading = []
for form in self.forms:
dim_id = 24 #form.cleaned_data['dimension_id']
reading_data = form.cleaned_data['reading']
target = get_dim_target(dim_id)
for x_reading in reading:
if int(x_reading) > int(target):
print True
raise forms.ValidationError("Reading larger than target")
else:
print False
reading.append(reading_data)
这是我的get_dim_target函数
def get_dim_target(dim_id):
target = Dimension.objects.values_list('target', flat=True).filter(id=dim_id)
return target
这是我在views.py
中的实际formsetdef update_inspection_vals(request, dim_id=None):
dims_data = Dimension.objects.filter(id=dim_id)
can_delete = False
dims = Dimension.objects.get(pk=dim_id)
sheet_data = Sheet.objects.get(pk=dims.sheet_id)
serial_sample_number = Inspection_vals.objects.filter(dimension_id=24).values_list('serial_number', flat=True)[0]
target = Dimension.objects.filter(id=24).values_list('target', flat=True)[0]
title_head = 'Inspect-%s' % dims.description
if dims.ref_dim_id == 1:
inspection_inline_formset = inlineformset_factory(Dimension, Inspection_vals, formset=BaseInspecitonFormSet, can_delete=False, extra=0, fields=('reading',), widgets={
'reading': forms.TextInput(attrs={'cols': 80, 'rows': 20})
})
if request.method == "POST":
formset = inspection_inline_formset(request.POST, request.FILES, instance=dims)
if formset.is_valid():
print True
new_instance = formset.save(commit=False)
for n_i in new_instance:
n_i.created_at = datetime.datetime.now()
n_i.updated_at = datetime.datetime.now()
n_i.save()
else:
print False
formset.errors
formset.non_form_errors()
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
else:
formset = inspection_inline_formset(instance=dims, queryset=Inspection_vals.objects.filter(dimension_id=dim_id).order_by('serial_number'))
return render(request, 'app/inspection_vals.html',
{
'formset': formset,
'dim_data': dims_data,
'title':title_head,
'dim_description': dims.description,
'dim_target': dims.target,
'work_order': sheet_data.work_order,
'customer_name': sheet_data.customer_name,
'serial_sample_number': serial_sample_number,
})
最后这是我的模板
<h1>Inspeciton Values</h1>
<div class="well">
<form method="post">
{% csrf_token %}
<table>
{{ formset.management_form }}
{% for x in formset.forms %}
<tr>
<td>
Sample Number {{ forloop.counter0|add:serial_sample_number }}
</td>
<td>
{{ x }}
</td>
</tr>
{% endfor %}
</table>
<input type="submit" value="Submit Values" class="btn-primary" />
</form>
</div>
</div>
答案 0 :(得分:0)
我想出来我必须将阅读更改为int以及我的目标然后presto它就像魅力一样。
class BaseInspecitonFormSet(BaseInlineFormSet):
def clean(self):
#if any(self.errors):
# return
reading = []
target = []
for form in self.forms:
dim_id = 24 #form.cleaned_data['dimension_id']
reading_data = form.cleaned_data['reading']
target_data = get_dim_target(dim_id)
reading.append(reading_data)
target.append(target_data)
x_t = target[0]
for x_r in reading:
if int(x_r) > int(x_t[0]):
print "Reading larger than target"
raise forms.ValidationError("Reading larger than target",code="bad")
else:
print "Reading is good"