2 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊
您不重復(fù)自己的解決方案是調(diào)用upload_design_to相關(guān)的Item
class Images(models.Model):
def upload_design_to(self, filename):
return self.item.upload_design_to(filename)
item = models.ForeignKey(Item, on_delete=models.CASCADE)
name = models.CharField(max_length=50, blank=True)
image = models.ImageField(blank=False, upload_to=upload_design_to)

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊
class Images(models.Model):
? ? def upload_design_to(self, filename):
? ? ? ? return f'{self.item.designer}/{self.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
關(guān)于您提出的錯(cuò)誤,您還可以為max_length文件字段設(shè)置一個(gè)屬性,這樣它就不會(huì)超過文件限制。您可以將其設(shè)置為 10000000 或足以容納您的文件的長(zhǎng)度。
添加回答
舉報(bào)