[Solved]-Store list of images in django model

14👍

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, ...)
👤Rohan

2👍

I would use the ManyToMany relationship to link your model with an image model. This is the way to aggregate ImageField as django does not have aggregate model field

def YourModel(models.Model):
    images = ManyToManyField(ImageModel)
    ...

def ImageModel(models.Model):
    img = ImageField()
    name ...

Maybe you need something more performant (this could lead to lots of horrible joins)

Leave a comment