Store list of images in django model

k3lf picture k3lf · Jul 16, 2013 · Viewed 7k times · Source

I am building a Django data model and I want to be able to store an array of ImageFields. Is it possible?

mainimage = models.ImageField(upload_to='img', null = True)
images = models.??

Thanks.

Answer

Rohan picture Rohan · Jul 16, 2013

Create another model to images and have foreignkey with your model.

def YourModel(models.Model):
    #your fields

def ImageModel(models.Model):
    mainimage = models.ImageField(upload_to='img', null = True)
    image = models.ForeignKey(YourModel, ...)