简体   繁体   中英

How to upload multiple images Using django?

I have Following Code in my models.py

class MyModel(models.Model):
    my_images= models.ImageField(upload_to='image/')

My forms.py is

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = "__all__"

and in my Views.py file is having following code

def myfunction(request):
    if request.method=='POST':
        form = MyForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponse('Data Inserted')
        else:
            return HttpResponse('form is invalid')

this Code is inserting single image into Database and directory.i want to Add multiple images into my directory and database. here i am making API so i am sending formdata(image) from postman app.
how to Upload multiple images?

ImageField can store only one image. If you need to upload multiple images, you should create separated ImageModel:

class MyModel(models.Model):
    pass

class ImageModel(models.Model):
    model_fk = models.ForeignKey(MyModel)
    img = models.ImageField(upload_to='image/', related_name='images')

Send your images in a list, then read that list in your view and create ImageModel instance for each image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM