I have a model with an image field. I have overridden the default
image field behaviour like so:

class ImageField(models.ImageField):
        def get_directory_name(self):
                longid = str( model_instance_id_number ).zfill(6)
                directory = longid[0:2] + "/" + longid[2:4]
                return
os.path.join(datetime.datetime.now().strftime(self.upload_to),
directory)

        def get_filename(self, filename):
                from django.utils.text import get_valid_filename
                longid = str( model_instance_id_number ).zfill(6)
                f = os.path.join(self.get_directory_name(),
get_valid_filename(longid[4:6] + os.path.splitext(filename)[1]))
                return os.path.normpath(f)

the idea is to organise the uploaded images in a two-level directory
structure according to the ID of the model instance in the database.
Problem is, obviously to do this I need to know the ID of the instance
( model_instance_id_number in above code). That makes it difficult to
create a new instance of the model, since the save() method saves the
file data to disk before it generates a database ID.

One naive solution is to set null=True on the image field in
models.py, call save() with no image specified, find the ID of the
database row, change the image to the right one, and call save again.

It's the best I can think of at present. Is there a better solution?


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

Reply via email to