Ad

How To Show Dynamic Drop Down In Django?

I want to show the Bank account number of an employee based on his saved banking details while adding salary. But now I am getting all employee's bank account number rather than just selected one. below is my forms.py

I tried to set the field in forms.py but I do not know how to filter using employee id in that.

class SalaryCreateForm(forms.ModelForm):
        class Meta:
            model = Salary_Rotation
            fields = ('Sal_Emp_Desig', 'Payment_Mode_of_Salary', 'Emp_Acc_number', 'Cheque_Number', 'Transaction_Number', 'P_Tax', 'Salary_Amount', 'Salary_Date')

    def __init__(self, *args, **kwargs):
        super(SalaryCreateForm,self).__init__(*args, **kwargs)
        self.fields['Sal_Emp_Desig'].queryset = Designation.objects.filter(Is_Del=0)
        self.fields['Emp_Acc_number'].queryset = Employee_Bank_Detail.objects.filter(Is_Del=0)

below is my template

<form class="form-group" method="POST">
                                        {% csrf_token %}
                                        <label class="control-label"> Employee Name:- </label>
                                        <input class="form-control" value="{{emp.Emp_Name}}" type="text" name="compnay" readonly/ >
                                        <label class="control-label"> Company Name:- </label>
                                        <input class="form-control" value="{{emp.Emp_company}}" type="text" name="compnay" readonly/ >

                                             <label>Bank Account</label>
                                            <select id="id_Emp_Acc_number" name="Emp_Acc_number" class="form-control" >
                                                    <option value="" selected="">---------</option>


                                            {% for acc_num in bank_acc_numbers %}
                                            <option value="{{acc_num.id}}" >{{ acc_num.Emp_Acc_num }}</option>
                                            {% endfor %}
                                        </select>
                                        {%endfor%}

                                        {{ form|crispy }}
                                        <button class="btn btn-rounded btn-success" type="submit">Add Experience</button>

                            {% for emp in employees%}
                                    <a target="_blank" rel="nofollow noreferrer" href="{% url 'employee-detail' emp.id %}" class="btn btn-rounded btn-brand ml-4">Cancel</a>
                                {%endfor%}
                                    </form>

below is my view.py

class SalaryCraeteView(LoginRequiredMixin,SuccessMessageMixin,CreateView):
    #permission_required = 'Company.add_company'
    model=Salary_Rotation
    success_message = " Salary added successfully!"
    reverse_lazy('employee-detail')
    login_url= "login"
    template_name = 'employee_salary_form.html'
    form_class = SalaryCreateForm
    def form_valid(self,form,**kwargs):
        form.instance.Sal_Crt_By = self.request.user
        form.instance.Is_Del =0

def get_context_data(self, **kwargs):
    kwargs['employees'] = Employee.objects.filter(pk=self.kwargs['pk'])
    kwargs['bank_acc_numbers'] = Employee_Bank_Detail.objects.filter(Emp_Bank_id=self.kwargs['pk']).filter(Is_Del=0)

    return super(SalaryCraeteView, self).get_context_data(**kwargs)

I want to show values in drop down based on employee's banking details, not all employee's banking details.

Ad

Answer

You may send the used_id from the view to the form using the method get_form_kwargs(), like

class SalaryCreateView(...):
...
    def get_form_kwargs(self):
        kwargs = super(SalaryCreateView, self).get_form_kwargs()
        kwargs['user_id'] = self.request.user.pk
        return kwargs

and apply this user_id in the filter, like

class SalaryCreateForm(forms.ModelForm):
...
    def __init__(self, *args, **kwargs):
        self.user_id=kwargs.pop('user_id')
        self.fields['Emp_Acc_number'].queryset = Employee_Bank_Detail.objects.filter(Is_Del=0, <user_id_field>=self.user_id)
Ad
source: stackoverflow.com
Ad