The easiest way I've found to do this is to subclass models.ImageField
from django.db.models and override the save_file method.  I have an
application where an artist uploads an image file - it makes a
thumbnail, and saves the file to a artist/book/chapter folder. I did
something like this (which basically moves the file after it's saved):

class KlangImageField(models.ImageField):

    def save_file(self, new_data, new_object, original_object, change,
rel):

        upload_field_name = self.get_manipulator_field_names('')[0]

        if rel:
             file_name = new_data[upload_field_name][0]["filename"]
        else:
             file_name = new_data[upload_field_name]["filename"]


        media_location = settings.MEDIA_ROOT
        file_location = os.path.join(media_location, file_name)
        thumbnail_location = os.path.join(media_location,
thumbnail_name(file_name))

        # if file already exists - delete the file and the thumbnail
        if os.path.exists(file_location):
             os.remove(file_location)
        if os.path.exists(thumbnail_location):
             os.remove(thumbnail_location)

        # go ahead and save the file in the default MEDIA_ROOT
        models.FileField.save_file(self, new_data, new_object,
original_object, change, rel)

        # make the thumbnail
        im = Image.open(file_location)
        im.thumbnail((128,128),Image.ANTIALIAS)
        im.save(thumbnail_location)


        # now move the file - it should go in the book/$chapter folder
        book = getattr(new_object, 'book')
        artist = book.artist
        chapter = getattr(new_object, 'chapter')

        file_dir = os.path.dirname(file_location)
        image_dir = os.path.join(file_dir, "images")

        if chapter is None:
             new_file_location = "%s/%s/%s/%s" % (image_dir,
artist.folder, book.folder, os.path.basename(file_location))
             new_thumbnail_location = "%s/%s/%s/%s" % (image_dir,
artist.folder, book.folder, os.path.basename(thumbnail_location))
        else:
             new_file_location = "%s/%s/%s/%s/%s" % (image_dir,
artist.folder, book.folder, chapter.folder,
os.path.basename(file_location))
             new_thumbnail_location = "%s/%s/%s/%s/%s" % (image_dir,
artist.folder, book.folder, chapter.folder,
os.path.basename(thumbnail_location))

        new_file_dir = os.path.dirname(new_file_location)

        if not os.path.exists(new_file_dir):
             os.makedirs(new_file_dir)

        os.rename(file_location, new_file_location)
        os.rename(thumbnail_location, new_thumbnail_location)

I hope this makes sense.  I can supply all the code (such as
make_thumbnail method) but I wanted to just post the relevent code.
The basic idea though is override save_file() and move the file after
it's saved (it's kludgy but I couldn't get it to work otherwise).  If
someone knows a better way that would be great - cause I don't
particulary like this

Rob


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to