我正在创建一个图像模型,其他模型将通过通用关系使用该模型。例如,Newsposts 和事件将有图像。下面是示例图像模型
class Image(models.Model):
description = models.CharField(max_length=500, null=True, blank=True)
image = models.ImageField(upload_to=get_storage_path)
content_type=models.ForeignKey(ContentType,on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey()
这会将图像仅存储在一个目录中。但是问题是我无法弄清楚如何编写一个动态upload_to
函数来将图像存储在相应的目录中,例如images/news/
和images/events
.