def image(instance, filename, image_size_type):
if image_size_type:
size = '/{}'.format(image_size_type)
else:
size = ''
return 'image/{0}{1}/{2}'.format(instance.am_id, size, filename)
class MyImageField(ImageField):
def __init__(self, image_size_type='', **kwargs):
self.image_size_type = image_size_type
super().__init__(**kwargs)
def generate_filename(self, instance, filename):
if callable(self.upload_to):
directory_name, filename = os.path.split(self.upload_to(instance, filename, self.image_size_type))
filename = self.storage.get_valid_name(filename)
return os.path.normpath(os.path.join(directory_name, filename))
return os.path.join(self.get_directory_name(), self.get_filename(filename))
class Image(models.Model):
image = MyImageField(upload_to=image, blank=True)
image_xs = MyImageField(upload_to=image, blank=True, image_size_type='xs')
image_sm = MyImageField(upload_to=image, blank=True, image_size_type='sm')
image_md = MyImageField(upload_to=image, blank=True, image_size_type='md')
am = models.ForeignKey(AnotherModel)