[Answered ]-Save uploaded files in subfolder depending on request

2👍

Yes, take a look at the documentation on upload_to.

You could do something like this, which includes the node id (defined as an integer in your model in the upload_to path:

def attachment_path_with_node(instance, filename):
    return "attachments/{}/{}".format(instance.node, filename)

class Attachment(models.Model):
    node = models.IntegerField(default=-1)
    file = models.FileField(upload_to=attachment_path_with_node)

0👍

Also path can be further customized like this:

document = models.FileField(upload_to='documents/%Y/%m/%d/')

which would upload to: MEDIA_ROOT/documents/2020/12/22/.

See more at https://simpleisbetterthancomplex.com/tutorial/2016/08/01/how-to-upload-files-with-django.html

Leave a comment