0

I use ImageField fields in my form and model to upload images, but I need to be multiple images and that just store one image and one reference in de database, this is my code:

#models.py
class mymodel(models.Model):
    imgs = models.ImageField(upload_to='uploads/%Y/%m/')

#forms.py
class myform(forms.Form):
    imgs = forms.ImageField(widget=forms.ClearableFileInput(attrs={'id': 'file', 'accept': 'image/*', 'multiple': True}))

#views.py
def myview(request):
    if request.method == 'POST':
        form = myform(request.POST, request.FILES)
            if form.is_valid():
                modelo = mymodel()
                modelo.imgs = form.cleaned_data.get('imgs')
                modelo.save()

When I do that only store one image even if I select 5 and in the database just store the path of the image that is stored

How can I store the 5 imagen on disk and in the database in one register store the references of the 5 images using something like ',' to separete

sorry for my bad english, If you need more information please ask me

1 Answer 1

0

mymodel.imgs is an ImageField which only supports a single field. You might try using an ArrayField (if you're using postgres for your database) where the child field is an ImageField. Or you'll need to use a form factory.

1
  • I nedd just one field but with the multiple attribute accept several images
    – M. Gar
    Nov 14, 2018 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.