[Django]-Django – Manipulating Data Posted from a form

3👍

def edit_change(request, change_id):

    ............

    if form.is_valid():
        change_id = form.cleaned_data['change_id']

        cmodel = form.save()

        if not change_id:
            cmodel.change_id = "TEST_NUM"
            cmodel.save()
        return redirect(changes)

    return render_to_response('editchange.html',context,   context_instance=RequestContext(request)

1👍

When you form.is_valid(), you create a form.data dict which form.save() then passes into the model associated with the form. If you add the in-between step of model_object = form.save(commit=False), you create a model instance, populated with all the form.data values. You can change and save this model instance as you need. E.g. model_object.change_id = "TEST_NUM". When you are done, you can finish with model_object.save(), never having need to use form.save()

More detail here: https://stackoverflow.com/questions/14982699/django-using-savecommit-false-to-add-user-id-to-a-form-dont-modify-save-or

👤Cole

0👍

I would redefine the clean method of the form, so that it makes the modification in the cleaned_data dict.

Leave a comment