Store list of images in django model
Asked Answered
S

2

11

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.

Scutch answered 16/7, 2013 at 9:34 Comment(0)
S
15

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, ...)
Straightedge answered 16/7, 2013 at 9:39 Comment(0)
S
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)

Submediant answered 16/7, 2013 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.