Store image list in django model

I am creating a Django data model and want to save an ImageFields array. Is it possible?

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

Thanks.

+4
source share
2 answers

Create another model for the images and enter the external key with your model into it.

 def YourModel(models.Model): #your fields def ImageModel(models.Model): mainimage = models.ImageField(upload_to='img', null = True) image = models.ForeignKey(YourModel, ...) 
+6
source

I would use the ManyToMany relation to associate your model with the image model. This is a way to combine ImageField since django does not have an aggregated model field

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

Maybe you need something more productive (this can lead to a lot of terrible joins)

+1
source

Source: https://habr.com/ru/post/1491609/


All Articles