我为我的项目模型创建了一个函数,其中有根据用户名和项目标题上传的图像。
现在我正在尝试向该项目添加更多图像并且图像有一个外键,但现在我不是随机创建将新图像上传到另一个文件夹,而是想将这些图像上传到之前根据确定的相同文件夹到同一个项目。
我试图改变以前的函数,但它返回了一个错误,upload_design_to() takes 1 positional argument but 2 were given我认为是因为我没有添加self到函数中,但我不知道用什么替换它。
下面的函数将更具描述性:
这是根据用户名和项目标题将图像上传到某个位置的模型和功能:
class Item(models.Model):
def upload_design_to(self, filename):
return f'{self.designer}/{self.title}/{filename}'
designer = models.ForeignKey(
User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
image = models.ImageField(blank=False, upload_to=upload_design_to)
现在我创建了一个新的图像模型来向该项目添加更多图像并希望将它们上传到同一个文件夹
class Images(models.Model):
def upload_design_to(filename):
return f'{Item.designer}/{Item.title}/{filename}'
item = models.ForeignKey(Item, on_delete=models.CASCADE)
name = models.CharField(max_length=50, blank=True)
image = models.ImageField(blank=True, upload_to=upload_design_to)
def __str__(self):
return self.name
qq_笑_17
喵喔喔
相关分类