Skip to main content

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

Project description

django-upload-to

codecov

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/models.py
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:

"attachments/the-file-name-uploaded.pdf"

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='user@email.com')

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

File in root folder

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

Define a folder structure

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

Generate a folder name using datetime formats from Python

>>> generator = upload_to.UploadTo(prefix=["pictures", "%Y"])
>>> generator(instance, "file.png")
"pictures/2023/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")
"b189dfdf25e640b2ba5c497472c20962.pdf"

Generate the folder path using the instance's attributes

>>> generator = upload_to.AttrUploadTo(attrs=["username"])
>>> generator(instance, "file.pdf")
"useremailcom/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")
"my_app/user/file.pdf"

Customize your upload paths

# my_app/models.py
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

Project details


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.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_upload_to-0.3.2-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file django_upload_to-0.3.2.tar.gz.

File metadata

  • Download URL: django_upload_to-0.3.2.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.0 CPython/3.11.3 Linux/5.15.0-1035-azure

File hashes

Hashes for django_upload_to-0.3.2.tar.gz
Algorithm Hash digest
SHA256 db5801c179f2f4e9b5c3bdea70b71d6a05b162f8dbaf5d5bd71ee577203b425c
MD5 5758bb54e3e1aea92b92c0961858a808
BLAKE2b-256 ba9bd6d694b5ef7a40b3cf5fea4d2b9c576b21705e05194d625f6333abfb280e

See more details on using hashes here.

File details

Details for the file django_upload_to-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: django_upload_to-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.0 CPython/3.11.3 Linux/5.15.0-1035-azure

File hashes

Hashes for django_upload_to-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e0ad8255a44cf689a175c4319cc169f0af617860ce0496a0c7a7aec69e067494
MD5 af9ff0d8ef0c791149c435ece2fb7370
BLAKE2b-256 1df6c783b161c6e8d0528bb7d2ec8eac47b490d9c578b35804cbf9e067e29c0a

See more details on using hashes here.

Supported by

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