How can I set picture to the image extension? I don't think "instance.picture = ext" works:
def pet_picture_upload(instance, filename): name, ext = os.path.splitext(filename) instance.picture = ext return '/usr/django/images/%s%s' % (instance.pk, ext) On May 5, 3:48 pm, Daniel Roseman <roseman.dan...@googlemail.com> wrote: > On May 5, 8:00 pm, Thierry <lamthie...@gmail.com> wrote: > > > > > I have the following model: > > > class Pet(models.Model): > > name = models.CharField(max_length=64) > > picture = models.ImageField(upload_to='/usr/django/images/') > > > def save(self, force_insert=False, force_update=False): > > // override the picture values > > super(Pet, self).save(force_insert, force_update) # Call the > > "real" save() method. > > > I want to achieve the following: > > - Any uploaded pet image will have the following name on the file > > system <pet_id>.<img_extension>, for example > > /usr/django/images/1.jpg, /usr/django/images/2.gif > > - How can can I retrieve the image extension, override the save method > > and store it in the column picture? > > > The Pet table will look something like: > > id name picture > > 1 Pluto jpg > > 2 Scooby gif > > > All the above is done on the admin side. > > Don't do this in the save method. Instead, define a custom upload > handler for the picture element. > > import os.path > > def pet_picture_upload(instance, filename): > name, ext = os.path.splitext(filename) > return '/usr/django/images/%s%s' % (instance.pk, ext) > > class Pet(models.Model): > name = models.CharField(max_length=64) > picture = models.ImageField(upload_to=pet_picture_upload) > > See the documentation for FileField, > here:http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.mod... > -- > DR. --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---