Question

Currently I am using something like:

initialValues={
    'textField1':'value for text field 1',
    'textField2':'value for text field 2',
    'imageField': someModel.objects.get(id=someId).logo
    }

form = myForm(initial=initialValues)

When I call myForm as above, the initial values are displayed as expected: the textField1, textField2 and imageField (with the options Currently: linkToImage, Clear check box and Change: )

But when I save the form, there is nothing saved in the imageField field (checking the database and I see the imageField field blank).

I know that I miss something here, but I cannot figure out what. Any tips?

Was it helpful?

Solution

I solved my issue by assigning

request.FILES['imageField']=someModel.objects.get(id=someId).logo

just before I save the form. Yeey

OTHER TIPS

You need to pass in the data to the form when creating it.

initialValues={
    'textField1':'value for text field 1',
    'textField2':'value for text field 2',
    'imageField': someModel.objects.get(id=someId).logo
    }
form = myForm(data=request.POST, initial=initialValues)

If you're not doing so already, I would suggest using class-based views. With a FormView you can easily override the get_initial_data() function, specify your values, and then let the view take care of what other things to pass on to the form to save it. If you're trying to save a model (which I think you are), check out the CreateView and UpdateView.

I could be more sure about this answer if I knew exactly how/when you were initializing that form.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top