django imagefield模型

时间:2012-03-07 01:35:29

标签: django imagefield

如果我希望图像保存在路径'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

1 个答案:

答案 0 :(得分:3)

您可以为upload_to提供callable。

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)