我对我的表单有这个观点:
if request.method == 'POST':
vehicle = VehicleForm(request.POST or None)
photos = PhotosFormSet(request.POST or None)
if vehicle.is_valid() and photos.is_valid():
new = vehicle.save()
photos = PhotosFormSet(request.POST, instance=new)
photos.save()
return HttpResponseRedirect('/vehicles/')
else:
prefix = "09-"
queryset = Vehicle.objects.all().order_by("stock_number")
if queryset == None:
last_rec = queryset.reverse()[0]
a = str(last_rec.stock_number)
b = int(a[-3:])+1
next = prefix+str(b)
else:
next = prefix+"001"
vehicle = VehicleForm(initial={'stock_number': next})
photos = PhotosFormSet(instance=Vehicle())
但是,当我尝试保存记录时,PhotosFormset
中的图片字段会显示错误This field is required.
PhotosFormset
被声明为PhotosFormSet = generic_inlineformset_factory(Photo, extra=5)
我在这里缺少什么?
答案 0 :(得分:3)
您似乎没有将上传的文件绑定到您的formset。您需要传入request.FILES以及request.POST:
photos = PhotosFormSet(request.POST, request.FILES, instance=new)
中的更多信息