[Answer]-Dynamic upload field arguments

1👍

Sure. Just check the docs for upload_to:

This may also be a callable, such as a function, which will be called
to obtain the upload path, including the filename.

Check this answer to see an example which you can easily adapt to your problem.

👤arie

0👍

One Example;

def image_path(self, uploaded_file_name):
    prefix = 'documents/'
    extension = os.path.splitext(uploaded_file_name)[-1]
    if self.pk != None:
        return prefix + str(self.pk) + extension
    else:
        tmp_name = str(uuid.uuid4())
        self.temp_image = prefix + tmp_name + extension
        return self.temp_image

image_upload = models.ImageField(upload_to=image_path, null=True, blank=True) 

You should use python functions.

Leave a comment