我試圖設置upload_to
ManyToMany字段slug的ImageField的值。Django ImageField upload_to從M2M關係值
這裏是我的模型:
def get_image_path(instance, filename):
return os.path.join('gallery', str(instance.albums.all()[0]), filename)
class Image(models.Model):
...
image = models.ImageField(upload_to=get_image_path)
...
可惜的是它不工作。當我嘗試保存圖像對象時,出現以下錯誤:
'Image' instance needs to have a primary key value before a many-to-many relationship can be used.
是否有任何正確的方法?
感謝您的幫助!
感謝您的快速和有用的答案! – mario199