Skip to main content

It generates dynamically a directory path and a file name for Django FileField

Project description


PyPI - License Pypi Version Codecov Building Python Versions Django Versions

It generates dynamically a directory path and a secure file name for Django FileField.

Main options:

  • Ready to use generators.
  • Generate secure file name without especial characters.
  • Generate file name using a uuid value.
  • Dynamically generate paths from the model instance.
  • Generate paths using Python datetime formats.

Get started

Install the django-upload-to in your virtual environment

$ pip install django-upload-to

Import it in your models file and add it as a upload_to argument in the FileField

# my_app/
from upload_to import UploadTo
from django.db import models

class MyModel(models.Model):
    attachment = models.FileField(upload_to=UploadTo("attachments"))

The path and file name generated will be like this:


How to use the ready-to-use classes

Consider the scenario below:

import upload_to
from django.db import models

class MyUser(models.Model):
    username = models.CharField(...)
    avatar = models.FileField(upload_to=<generator>)

instance = MyUser(username='')

Replace the <generator> fragment by the generators as showed below:

File in root folder

>>> generator = upload_to.UploadTo()
>>> generator(instance, "file.pdf")

Define a folder structure

>>> generator = upload_to.UploadTo(prefix=["files", "documents"])
>>> generator(instance, "file.pdf")

Generate a folder name using datetime formats from Python

>>> generator = upload_to.UploadTo(prefix=["pictures", "%Y"])
>>> generator(instance, "file.png")

Replace the file name by an hexadecimal uuid value

# 4. replace file name by a uuid value
>>> generator = upload_to.UuidUploadTo()
>>> generator(instance, "file.pdf")

Generate the folder path using the instance's attributes

>>> generator = upload_to.AttrUploadTo(attrs=["username"])
>>> generator(instance, "file.pdf")

Generate the folder path using the app_label and the model_name from the instance's meta options

>>> generator = upload_to.ModelUploadTo()
>>> generator(instance, "file.pdf")

Customize your upload paths

# my_app/
import upload_to
from django.db import models

def my_upload_generator(instance, filename):
    filename = upload_to.uuid_filename(filename)
    path = upload_to.options_from_instance(instance)
    return upload_to.upload_to(path, filename)

class MyProfile(models.Model):
    user = models.OneToOneField(...)
    avatar = models.FileField(upload_to=my_upload_generator)

Useful links

  1. Documentation
  2. Changelog
  3. PyPi Page

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_upload_to-0.3.4.tar.gz (5.1 kB view hashes)

Uploaded source

Built Distribution

django_upload_to-0.3.4-py3-none-any.whl (5.5 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page