[Answered ]-Form values are not overwritten when changed โ€“ they are stuck at their default values

1๐Ÿ‘

โœ…

You are setting the attributes of the form, not of the instance wrapped in the form. You should alter this to:

@login_required
def my_view(request):
    instance = my_model(user=request.user)
    if request.method == 'POST':
        form = my_model_form(request.POST, instance = instance)
        if form.is_valid():
            # Field1 and field2 is already in the form (its the input)
            # Do some back-end operations to get values for the remaining fields
            df = some_util_function()
            #       โ†“ the instance of the form
            form.instance.field3 = df['field3']
            form.instance.field4 = df['field4']
            form.instance.field5 = df['field5']
            form.save()

            return redirect("my_html")
    else:
        form = my_model_form()

    context = {
        'form': form
    }
    return render(request, 'discounttracker/my_html.html', context=context)

Leave a comment