Alejandro Veintimilla
Alejandro Veintimilla

Reputation: 11523

Django upload many images and save to model

I have seen similar questions, but they are old. I wonder if there is a new better approach to this process.

I have a form that uploads many images at once. ¿How do I process them with django and create one model object per uploaded image?

I have a model with an Image field:

class XModel(models.Model):
    image = ImageField(...)

Can I use a ModelForm? Should I use a normal forms.Form and a MemoryUploadedFile?

Any advice is welcome.

Upvotes: 1

Views: 134

Answers (1)

Alejandro Veintimilla
Alejandro Veintimilla

Reputation: 11523

Ok, this is how I did it. Maybe there is a better way but anyway:

    if request.method == "POST":
        files = request.FILES.getlist('images')
        for x in files:
            XModel.objects.create(image=x)
        return HttpResponse('Yeii')

Upvotes: 1

Related Questions