It generates dynamically a directory path and a file name for Django FileField
Project description
django-upload-to
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)
File size validators
The library also provides file size validators to restrict upload file sizes.
MaxSizeValidator
Validates that the file size is less than the specified limit.
# my_app/models.py
from upload_to import UploadTo, MaxSizeValidator, MB
from django.db import models
class MyModel(models.Model):
attachment = models.FileField(
upload_to=UploadTo("attachments"),
validators=[MaxSizeValidator(5 * MB)] # Max 5MB
)
MinSizeValidator
Validates that the file size is greater than the specified limit.
# my_app/models.py
from upload_to import UploadTo, MinSizeValidator, KB
from django.db import models
class MyModel(models.Model):
attachment = models.FileField(
upload_to=UploadTo("attachments"),
validators=[MinSizeValidator(1 * KB)] # Min 1KB
)
Using both validators together
# my_app/models.py
from upload_to import UploadTo, MaxSizeValidator, MinSizeValidator, KB, MB
from django.db import models
class MyModel(models.Model):
attachment = models.FileField(
upload_to=UploadTo("attachments"),
validators=[
MinSizeValidator(1 * KB), # Min 1KB
MaxSizeValidator(5 * MB), # Max 5MB
]
)
Available size units
The library provides convenient size unit constants:
KB= 1,024 bytesMB= 1,048,576 bytesGB= 1,073,741,824 bytesTB= 1,099,511,627,776 bytesPB= 1,125,899,906,842,624 bytes
Useful links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_upload_to-1.0.0.tar.gz.
File metadata
- Download URL: django_upload_to-1.0.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.12.11 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e07409fec8a34020c927202265bb43277933326c3d43c432d6cb94bd1c9a7f0
|
|
| MD5 |
de11d8edb9bf2b9d1745a54b8fd73edc
|
|
| BLAKE2b-256 |
cc6a58d17a7ce9ae498d0b83c715004d531d1dc8263786ba13b25fca73927d6c
|
File details
Details for the file django_upload_to-1.0.0-py3-none-any.whl.
File metadata
- Download URL: django_upload_to-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.12.11 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cef28f6647615713158a19af787fc9fdb7c86e1f2fef5bbc5b51b94b71ef423
|
|
| MD5 |
bbe07657e08b12121888c072bf3b8083
|
|
| BLAKE2b-256 |
927ef872559b009decc5ff3f8d22a3e81d8e6c868e46831783e92bdedfcb952c
|