Lewis
Lewis

Reputation: 147

django imagefield model

how can I define it in the model if I want the image to save in the path 'img/'currentdateMonthYear_ + category_+ the given name. The category depend on what I choose?

currently I describe my model as such:

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

Upvotes: 1

Views: 2483

Answers (1)

Sander van Leeuwen
Sander van Leeuwen

Reputation: 3033

You can provide a callable to 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)

Upvotes: 3

Related Questions