1👍
✅
Just use a ManyToManyField
to store the relationship between a Post
(or Item
) and an Image
and then iterate across that. Have models.py like so:
class Image(models.Model):
image = models.ImageField(upload_to='images')
class Post(models.Model):
body = models.TextField()
images = models.ManyToManyField(Image)
And elsewhere, pull the set of images
from a Post
instance and iterate across that:
my_post = Post.objects.first()
for image in my_post.images:
print image.url
Source:stackexchange.com