我在带有 Cloudinary 包的 Django 应用程序中定义了如下图像上传,

class Photo(models.Model):
    photo = CloudinaryField('image')

我想让这个领域上传多张图片。我该怎么做呢?

最佳答案

包含多张图像的照片成为相册或照片库。我会改造如下:

class PhotoAlbum(models.Model):
    name = models.CharField()  # or whatever a photo album has

class Photo(models.Model):
    file = CloudinaryField('image')
    album = models.ForeignKey(PhotoAlbum, on_delete=models.CASCADE, related_name='photos')

用法示例:
>>> album = PhotoAlbum.objects.create(name='myalbum')
>>> photo = Photo.objects.create(album=album, image=...)

现在, Photo 知道它的 PhotoAlbum :
>>> photo = Photo.objects.first()
>>> photo.album
<PhotoAlbum: myalbum>
PhotoAlbum 跟踪所有 Photo :
>>> album = PhotoAlbum.objects.first()
>>> album
<PhotoAlbum: myalbum>
>>> album.photos.all()
<QuerySet [<Photo: Photo-1>]>
>>> album == Photo.objects.first().album
>>> True
>>> Photo.objects.first() == album.photos.first()
>>> True

关于python - django 中的多云多图像上传?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48914518/

10-13 03:53