Created
February 22, 2012 14:55
-
-
Save joshourisman/1885431 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def model(models.Model): | |
file = models.FileField(upload_to='my/file/uploads/') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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