Can it be used instead of ImageField?
varqasim opened this issue · comments
This works for images as well doesn't it?
Sure! This works for media types (images, videos, sound files etc) just as well as for plain file types! Also allows you to mix them during one upload (e.g. attach an image and a pdf document at the same time).
If you use MultiFieldField on your form for a ImageField model field, uploaded files will not be validated to ensure that they are image files.
MultiFileField class definition in fields.py needs to inherit from forms.ImageField (instead of forms.FileField) to keep this validation functionality.
@tnakasaki is that already done, or do I need to do that my self? Excuse my 'noobness'
edit: So by changing it to forms.ImageField that would do the trick, or is there anymore coding for that to happen?
edit2: I changed up forms.FileField to forms.ImageField and now my views look like this
def sell(request):
if request.method == 'POST':
sell_form = AdvForm(data=request.POST)
sell_image_form = AdvImageForm(data=request.POST)
if sell_form.is_valid() and sell_image_form.is_valid():
human = True
form_sell = sell_form.save(commit=False)
form_sell.user = request.user
form_sell.save()
form_sell_image = sell_image_form.save(commit=False)
form_sell_image.user = form_sell.user
form_sell_image.post = form_sell
else:
print sell_form.errors, sell_image_form.errors
else:
sell_form = AdvForm()
sell_image_form = AdvImageForm()
return render(request, 'sell.html', {'sell_form': sell_form, 'sell_image_form': sell_image_form}, )
class UploadView(FormView):
template_name = 'sell.html'
form_class = AdvImageForm
success_url = '/'
def form_valid(self, form):
for each in form.cleaned_data['adv_images']:
AdvImages.objects.create(adv_images=each)
return super(UploadView, self).form_valid(form)
Selecting multiple images is possible but when submitting the form the image field says it is required, so that means they aren't even validating I guess?
@qasimalbaqali
You can define your own new image form field like this:
class MultiImageField(MultiFileField, forms.ImageField):
pass
Then you'll have a multiple image upload field (thanks to MultiFileFIeld) that inherits the validation properties of forms.ImageField
I think so anyway...
@qasimalbaqali I think you need to bind the data to your form object this way
https://docs.djangoproject.com/en/1.8/ref/forms/api/#binding-uploaded-files