我正在构建一个django应用程序,用户可以在其中上传多张图像并将其存储为相册。
在我当前的实现中,我有两个模型Photo
和Album
。在Album
模型中,我有一个Photo
模型的多对多字段。因此,用户可以在Album
模型中选择多张照片。
有没有一种方法可以实现Album
模型中的照片选择器,而不是简单的多对多字段?
这是我的代码:
class Photo(models.Model) :
def get_gallery_path(self, filename):
ext = filename.split('.')[-1]
filename = "%s.%s" % (uuid.uuid4(), ext)
return 'static/uploads/images/gallery/' + filename
uploader = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField(default=date.today)
image = ProcessedImageField(default='', verbose_name='Image', upload_to=get_gallery_path,validators=[validate_file_size], **processed_image_field_specs)
caption = models.CharField(max_length=200, null=True, blank=True)
def __str__(self):
return str(self.id) + str(self.uploader.username) + str(self.date)
class Meta:
verbose_name_plural = "Photos"
verbose_name = "Photo"
class Album(models.Model) :
title = models.CharField(max_length=200)
uploader = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField(default=date.today)
description = models.TextField(null=True, blank=True)
tags = models.ManyToManyField(Tag, blank=True)
category = models.ManyToManyField(Category, blank=True)
photos = models.ManyToManyField(Photo, blank=True)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = "Albums"
verbose_name = "Album"
PS:我仅使用内置的管理界面来存储数据。