Skip to content

Instantly share code, notes, and snippets.

@joshourisman
Created February 22, 2012 14:55
Show Gist options
  • Save joshourisman/1885431 to your computer and use it in GitHub Desktop.
Save joshourisman/1885431 to your computer and use it in GitHub Desktop.
import random, string
from django.contrib.contenttypes.models import ContentType
def get_path(instance, filename):
ctype = ContentType.objects.get_for_model(instance)
model = ctype.model
app = ctype.app_label
extension = filename.split('.')[-1]
dir = "site"
if model == "job":
dir += "/pdf/job_attachment"
else:
dir += "/img/%s" % app
if model == "image_type_1":
dir += "/type1/%s" % instance.category
elif model == "image_type_2":
dir += "/type2"
elif model == "restaurant":
dir += "/logo"
else:
dir += "/%s" % model
chars = string.letters + string.digits
name = string.join(random.sample(chars, 8), '')
return "%s/%s.%s" % (dir, name, extension)
def model(models.Model):
file = models.FileField(upload_to='my/file/uploads/')
from myproject.lib.files import get_path
def model(models.Model):
file = models.FileField(upload_to=get_path)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment