Custom Search

Tuesday, October 25, 2011

howto django uploading files to dynamic path

def get_upload_path(instance, filename):
    """
        Get upload path
        eg: upload/1/2011/10/16/09/00/12/oauth_application.png
    """
    today = datetime.utcnow()
    date_time_str = today.strftime("%Y/%m/%d/%H/%M/%S")
    path = "%s/%s/%s/%s" %(storage, instance.user.id, date_time_str, filename)
#    print "---------", path
    return path


class MultiuploaderImage(models.Model):
    """Model for storing uploaded photos"""
    filename = models.CharField(max_length=60, blank=True, null=True)
    image = models.FileField(upload_to=get_upload_path)
    key_data = models.CharField(max_length=90, unique=True, blank=True, null=True)
    upload_date = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(User)

No comments:

Post a Comment