2

I have a form with a few fields. Most of the time, people only need to change one field every time they submit, not all, so I would like to change it in such a way that upon submission, only the employee field is cleared, but the Work Area choice and station (if there are any available) remain selected.

How could I change the way the form is handled so it is not completely cleared upon submission? I know the form = WarehouseForm() is what clears it, but I'm not sure how I could just specify I only want the employee_number field cleared.

views.py

def enter_exit_area(request):
    enter_without_exit = None
    exit_without_enter = None

    if request.method == 'POST':
        form = WarehouseForm(request.POST)

        if form.is_valid():
            emp_num = form.cleaned_data['employee_number']
            area = form.cleaned_data['work_area']
            station = form.cleaned_data['station_number']

            if 'enter_area' in request.POST:
                # Submission logic
                form = WarehouseForm()

            elif 'leave_area' in request.POST:
                # Submission logic ..

                form = WarehouseForm()
    else:
        form = WarehouseForm()
    return render(request, "operations/enter_exit_area.html", {
        'form': form,
        'enter_without_exit': enter_without_exit,
        'exit_without_enter': exit_without_enter,
    })
Daniel Holmes
  • 1,952
  • 2
  • 17
  • 28
  • Check this thread out: https://stackoverflow.com/questions/813418/django-set-field-value-after-a-form-is-initialized – Joe Dec 04 '19 at 15:28

3 Answers3

2

You can use the initial argument:

if 'enter_area' in request.POST:
    # Submission logic
    form = WarehouseForm(initial={'employee_number': '', 'work_area': area, 'station_number': station})

Note the employee_number field is blank.

You haven't included your submission logic, so your implementation might be slightly different. But the principle is the same.

Daniel Holmes
  • 1,952
  • 2
  • 17
  • 28
0
form_obj = form.save(commit=False)
form_obj.employee_number = '' # if you want to clear employee_number field
form_obj.save()


Note:- 'employee_number' field should be null=True in models.py, so that you can clear the field

For Example:- 

def enter_exit_area(request):
    enter_without_exit = None
    exit_without_enter = None

    if request.method == 'POST':
        form = WarehouseForm(request.POST)

        if form.is_valid():
            emp_num = form.cleaned_data['employee_number']
            area = form.cleaned_data['work_area']
            station = form.cleaned_data['station_number']

            if 'enter_area' in request.POST:
                # Submission logic
                form_obj = form.save(commit=False)
        form_obj.employee_number = '' # if you want to clear employee_number field

            elif 'leave_area' in request.POST:
                # Submission logic ..
        form_obj = form.save(commit=False)
        form_obj.employee_number = '' # if you want to clear employee_number field

    form_obj.save()
    else:
        form = WarehouseForm()

    return render(request, "operations/enter_exit_area.html", {
        'form': form,
        'enter_without_exit': enter_without_exit,
        'exit_without_enter': exit_without_enter,
    })
Mukul Kumar
  • 2,033
  • 1
  • 7
  • 14
0

If it is input type textbox then you can also do it in javascript something like below : eg : document.getElementById("formFieldId").value = '';