내가 직면하는 문제가 있습니다. 관련 데이터가 포함 된 학생을 Excel에 업로드해야합니다. 또한 사용자 입력으로 학생 배치를 가져야합니다. 다음은 내 코드는 다음과 같습니다Django 업로드 양식, 오류를 일으키는 추가 입력란
Views.py
def import_student(request):
this_tenant=request.user.tenant
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
def choice_func(row):
data=student_validate(row, this_tenant, batch_selected)
return data
if form.is_valid():
data = form.cleaned_data
batch_data= data['batch']
batch_selected=Batch.objects.for_tenant(this_tenant).get(id=batch_data)
with transaction.atomic():
try:
request.FILES['file'].save_to_database(
model=Student,
initializer=choice_func,
mapdict=['first_name', 'last_name', 'dob','gender','blood_group', 'contact', 'email_id', \
'local_id','address_line_1','address_line_2','state','pincode','batch','key','tenant','user'])
return redirect('student:student_list')
except:
transaction.rollback()
return HttpResponse("Error")
else:
print (form.errors)
return HttpResponseBadRequest()
else:
form = UploadFileForm(tenant=this_tenant)
return render(request,'upload_form.html',{'form': form,})
Forms.py 그러나 오류가, (I 오류를 인쇄하고 있습니다) 제출에 표시되는
class UploadFileForm(forms.Form):
file = forms.FileField()
batch = forms.ModelChoiceField(Batch.objects.all())
def __init__(self,*args,**kwargs):
self.tenant=kwargs.pop('tenant',None)
super (UploadFileForm,self).__init__(*args,**kwargs) # populates the post
self.fields['batch'].queryset = Batch.objects.for_tenant(self.tenant).all()
self.helper = FormHelper(self)
self.helper.add_input(Submit('submit', 'Submit', css_class="btn-xs"))
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-sm-2'
self.helper.field_class = 'col-sm-4'
형식은 다음과 같습니다.
<ul class="errorlist"><li>batch<ul class="errorlist"><li>Select a valid choice. That choice is not one of the available choices.</li></ul></li></ul>
배치 필드를 제거하면 양식이 잘 작동합니다. 아무도 이것으로 나를 도울 수 있습니까? 다른 값과 이름 (대신 -------)와
<option value="">---------</option>
다른 옵션을 선택하기되지 않습니다
게시물은 항상 첫 번째 옵션을 받고있다. 비록 클라이언트가 실제로 다른 옵션을 선택하고 있습니다.
지금, 나는 오류 때문에 다음 줄의 일어나고있는 것으로 나타났습니다이없이
self.fields['batch'].queryset = Batch.objects.for_tenant(self.tenant).all()
양식은 잘 작동합니다. 그러나이 선은 필수입니다. 쿼리 세트는 동적으로 업데이트되어야합니다. 어떻게 할 수 있습니까?
와우,을. 감사합니다 Amin !! – Sayantan