django-admin-tabs
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-admin-tabs in your virtual environment
$ pip install django-admin-tabs
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
Metadata
Release files for django-admin-tabs 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_admin_tabs-0.0.1.tar.gz | 6.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_admin_tabs-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.4 kB
Release files / django_admin_tabs-0.0.1.tar.gz
| Download URL | django_admin_tabs-0.0.1.tar.gz |
|---|---|
| Size | 6.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
85f284af8fcb9b9e8c51de6e35434ce0e2046c494bb748a258783c34bbd0bfa7
|
|
BLAKE2b-256 checksum How to use checksums |
5433207d310c7184c3e8714c697ada115b159d66a01093ef211caf77280a0e41
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.12.1 Linux/6.2.0-1018-azure
|
Release files / django_admin_tabs-0.0.1-py3-none-any.whl
| Download URL | django_admin_tabs-0.0.1-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
73f08150e857555d91d65746ddddee8cf0dcad1e34f9f49a3b2cde1a390a3d86
|
|
BLAKE2b-256 checksum How to use checksums |
5a69a80b84a894ae5270d8729fba8d68eb771f288fd68b0072d6dfda45737eea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.12.1 Linux/6.2.0-1018-azure
|