Django upload form, additional field causing error

316 Views Asked by At

There is an issue I'm facing. I need to upload an excel of students containing their related data. I also need to have, as an user-input, the batch of the student. Following are my codes:

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

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'

However, the error, (I'm printing the error) being shown on submitting the form is:

<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>

If I remove the batch field, the form is working great. Can anyone help me with this?

The post is always getting the first option, which is :

<option value="">---------</option>

The other options with other values and name (instead of -------) is not getting selected. Although, the client is actually selecting the other options.

Now, I've found that the error is happening because of the following line:

self.fields['batch'].queryset = Batch.objects.for_tenant(self.tenant).all()

Without this, the form works great. But this line is a must. The queryset has to be dynamically updated. How can this be done?

1

There are 1 best solutions below

1
Amin Sabbaghian On BEST ANSWER

You must pass the tenant argument when you are saving the form, otherwise its queryset will be empty and your choice not getting selected.

This code must work:

if request.method == "POST":
    form = UploadFileForm(request.POST, request.FILES, tenant=this_tenant)