1

如果我希望图像保存在路径 'img/'currentdateMonthYear_ + category_+ 给定名称中,如何在模型中定义它。类别取决于我选择什么?

目前我这样描述我的模型:

class Image(models.Model):
    title       = models.CharField(max_length=50)
    description     = models.TextField()
    hits        = models.IntegerField()
    timestamp   = models.DateTimeField(auto_now_add=True)
    thumb       = models.ImageField(upload_to='photos...
    tag         = models.ForeignKey(Tag)
    artist      = models.ForeignKey(User)
    storage     = models.ForeignKey(Storage)
    market      = models.ForeignKey(Market)
    Category    = models.ForeignKey(Category)
    Log         = models.ForeignKey(Log)

    def __unicode__(self):
        return self.title
4

1 回答 1

3

您可以提供一个可调用的 upload_to。

def get_image_path(instance, filename):
    return os.path.join('img', str(instance.category), filename)

class Image(models.Model):
    thumb = models.ImageField(upload_to=get_image_path)
于 2012-03-07T12:25:02.070 回答