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.
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, ...)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With