views:

226

answers:

1

Hay guys, I've wrote a simple upload method for my pictures

class Picture(models.Model):
    path = models.CharField(max_length=200)
    filename = models.CharField(max_length=200)
    car = models.ForeignKey('Car')
    thumb_path = models.CharField(max_length=200)
    created_on = models.DateField(auto_now_add=True)
    updated_on = models.DateField(auto_now=True)

    def save(self):
        if not self.id:
            thumb_size = 128, 128
            thumb_path = "assests/uploads/thumb"+self.filename
            t = Image.open(self.path)
            t.thumbnail(thumb_size,Image.ANTIALIAS)
            t.save(thumb_path, "JPEG")
        self.thumb_path = thumb_path
        super(Picture, self).save()

    def delete(self):
        os.unlink(self.thumb_path)
        os.unlink(self.path)
        super(Picture, self).delete()

As you can see this isn't the best method, i want to move on to the ImageField() to do most of my work, but i still want the flexiability to create a thumbnail and random filename.

would i need to create another model for PictureThumbnail? I don't really want to use any 3rd part extensions.

how could i use ImageField to make this work? All the iamges are gong to be uploaded to /assests/uploads/

Thanks

+1  A: 

You could use an adapted version of:

http://www.djangosnippets.org/snippets/1100/

Or depending on what exactly you need to do you can consider a template filter based approach something like this:

http://www.djangosnippets.org/snippets/1887/

Frozenskys
I'm using the first link. Howver ImageField() doesnt actually upload anything? It adds data to the db but no files are uploaded? Any ideas?
dotty
does the image field upload properly without the rest of the code? NOTE: the upload_to parameter is a local filesystem path that will be appended to your MEDIA_ROOT setting, and the user your python instance is running as must have write access to this.
Frozenskys