1

Let's say i have many models like that:

class ExampleModel(models.Model):
    photo = models.ImageField(upload_to='photos/')
    image = models.ImageField(upload_to='images/')

class AnotherExampleClass(models.Model):
    another_image = models.ImageField(upload_to='another_images/')

So, it's many different models with ImageFields, which have different field names. What i want, is to somehow override all upload_to like that:

upload_to = '%s/%s' % (upload_to, self.id)

for instance, if i upload picture to another_image field, file path would be:

'another_images/1/picture.jpg'

I realize that i need to create some CustomImageField based on models.ImageField. But how exactly achive that with minimal pain?

nukl
  • 10,073
  • 15
  • 42
  • 58
  • see http://stackoverflow.com/questions/8535189/django-imagefield-how-to-make-upload-to-parameter-dependent-on-self-id/8535432 and https://docs.djangoproject.com/en/dev/topics/http/file-uploads/#basic-file-uploads and http://stackoverflow.com/questions/1190697/django-filefield-with-upload-to-determined-at-runtime – Alexey Savanovich Dec 21 '11 at 08:41

1 Answers1

3

upload_to can be a callable. You know what to do next. :-)

DrTyrsa
  • 31,014
  • 7
  • 86
  • 86