Skip to content

Instantly share code, notes, and snippets.

@tachang
Created May 7, 2012 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tachang/2629805 to your computer and use it in GitHub Desktop.
Save tachang/2629805 to your computer and use it in GitHub Desktop.
ImageKit Save Method
# Image inherits from imagekit.models.ImageModel
class Image(ImageModel):
def retrieve_upload_path(image, filename):
fname, dot, extension = filename.rpartition('.')
return "images/%s.%s" % ( image.uuid, extension)
name = models.CharField(max_length=100, blank=True)
image = models.ImageField(upload_to=retrieve_upload_path)
num_views = models.PositiveIntegerField(editable=False, default=0)
uploaded = models.DateTimeField(auto_now=True, blank=True)
uuid = models.CharField(max_length=64, blank=True)
def __unicode__(self):
return "%s [%s]" % (self.name, self.image)
def save(self, *args, **kwargs):
if( not self.uuid ):
self.uuid = str(uuid.uuid4())
if( not self.name ):
self.name = self.image.name
self.uploaded = datetime.now()
super(Image, self).save(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment