I have this piece of code:
vehicle = get_object_or_404(Vehicle, stock_number=stock_number)
if request.method == 'POST':
vehicle_form = VehicleForm(request.POST, instance=vehicle)
photos = PhotosFormSet(request.POST, request.FILES, instance=vehicle)
if vehicle_form.is_valid() and photos.is_valid():
vehicle = vehicle_form.save()
photos.save()
request.user.message_set.create(message='The vehicle "%s" was edited
successfully.' % vehicle.__str__())
return HttpResponseRedirect("/vehicles/details/%s/" % stock_number)
else:
vehicle_form = VehicleForm(instance=vehicle)
photos = PhotosFormSet(instance=vehicle)
return render_to_response('vehicles/vehicles-add-edit.html',
{'vehicle_form':vehicle_form, 'photos': photos,},
context_instance=RequestContext(request))
The forms are initialized correctly except the ImageField
in PhotosFormSet
.