Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6208527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:49:26+00:00 2026-05-24T05:49:26+00:00

I have a Django model with multiple ImageFields and use a callable to determine

  • 0

I have a Django model with multiple ImageFields and use a callable to determine the upload path. I want to include the originating upload field’s name in the upload path, in this case tiny, small, medium or press.

The only way I could think of was to create a pre_save receiver which replaces file.name with a uuid. Then the upload_to callable finds the match by comparing it to filename. Isn’t there a less hacky way of doing this?

class SomeDjangoModel(models.Model):

    IMAGE_SIZES = ('tiny', 'small', 'medium', 'press')

    def image_path(self, filename):
        """ Example return: [some-django-model]/[medium]/[product1].[jpg] """
        size = None
        for field_name in self.IMAGE_SIZES:
            field_fn = getattr(getattr(self, field_name), 'name', '')
            if field_fn == filename.rpartition('/')[2]:
                size = field_name
                break

        return u'{}/{}/{}.{}'.format(
            slugify(self._meta.verbose_name),
            size or 'undetermined',
            self.slug,
            filename.rpartition('.')[2].lower(),
        )

    tiny = models.ImageField(upload_to=image_path, blank=True, null=True)
    small = models.ImageField(upload_to=image_path, blank=True, null=True)
    medium = models.ImageField(upload_to=image_path, blank=True, null=True)
    press = models.ImageField(upload_to=image_path, blank=True, null=True)

The pre_save receiver:

@receiver(pre_save, sender=SomeDjangoModel)
def set_unique_fn(sender, instance, **kwargs):
    """ Set a unique (but temporary) filename on all newly uploaded files. """

    for size in instance.IMAGE_SIZES:
        field = getattr(instance, '{}_img'.format(size), None)
        if not field:
            continue
        fieldfile = getattr(field, 'file', None)
        if isinstance(fieldfile, UploadedFile):
            fieldfile.name = u'{}.{}'.format(
                uuid.uuid4().hex,
                fieldfile.name.rpartition('.')[2],
            )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:49:27+00:00Added an answer on May 24, 2026 at 5:49 am

    You can change image_path() so that it returns a callable which already knows the size:

    def image_path(size):
        def callback(self, filename)
            """ Example return: [some-django-model]/[medium]/[product1].[jpg] """
            return u'{}/{}/{}.{}'.format(
                slugify(self._meta.verbose_name),
                size,
                self.slug,
                filename.rpartition('.')[2].lower(),
            )
        return callback
    
    class SomeDjangoModel(models.Model):
        tiny = models.ImageField(upload_to=image_path('tiny'), blank=True, null=True)
        small = models.ImageField(upload_to=image_path('small'), blank=True, null=True)
        medium = models.ImageField(upload_to=image_path('medium'), blank=True, null=True)
        press = models.ImageField(upload_to=image_path('press'), blank=True, null=True)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have a django model as following class Project(models.Model) name=models.CharField(max_length=200) class Application(models.Model) proj=models.ForeignKey(Project, null=True,
I have a django model: class Book(models.Model): [..] and I want to have the
I have the following django model (mapped to table 'A'): class A(models.Model): name =
I have a Django model with a large number of fields and 20000+ table
I have worked a bit with Django and I quite like its project/applications model
(Django 1.x, Python 2.6.x) I have models to the tune of: class Animal(models.Model): pass
I have written a Django app that makes use of Python threading to create
When I have a given django model class like this: class BaseClass(models.Model): some_field =
I have a Django project, that has a Address model. This is used in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.