Write advanced filename patterns using the Format String Syntax.
Project description
Write advanced filename patterns using the Format String Syntax.
Getting Started
Installation
pip install django-dynamic-filenames
Samples
Basic example:
from django.db import models
from dynamic_filenames import FilePattern
upload_to_pattern = FilePattern(
filename_pattern='{app_label:.25}/{model_name:.30}/{uuid:base32}{ext}'
)
class FileModel(models.Model):
my_file = models.FileField(upload_to=upload_to_pattern)
Auto slug example:
Features
Field names
- ext
File extension including the dot.
- name
Filename excluding the folders.
- model_name
Name of the Django model.
- app_label
App label of the Django model.
- instance
Instance of the model before it has been saved. You may not have a primary key at this point.
- uuid
UUID version 4 that supports multiple type specifiers. The UUID will be the same should you use it twice in the same string, but different on each invocation of the upload_to callable.
The type specifiers allow you to format the UUID in different ways, e.g. {uuid:x} will give you a with a hexadecimal UUID.
The supported type specifiers are:
- s
String representation of a UUID including dashes.
- i
Integer representation of a UUID. Like to UUID.int.
- x
Hexadecimal (Base16) representation of a UUID. Like to UUID.hex.
- X
Upper case hexadecimal representation of a UUID. Like to UUID.hex.
- base32
Base32 representation of a UUID without padding.
- base64
Base64 representation of a UUID without padding.
All type specifiers also support precisions to cut the string, e.g. {{uuid:.2base32}} would only return the first 2 characters of a Base32 encoded UUID.
Type specifiers
You can also use a special slug type specifier, that slugifies strings.
Example:
from django.db import models
from dynamic_filenames import FilePattern
upload_to_pattern = FilePattern(
filename_pattern='{app_label:.25}/{model_name:.30}/{instance.title:.40slug}{ext}'
)
class FileModel(models.Model):
title = models.CharField(max_length=100)
my_file = models.FileField(upload_to=upload_to_pattern)
Slug type specifiers also support precisions to cut the string. In the example above the slug of the instance title will be cut at 40 characters.
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
File details
Details for the file django-dynamic-filenames-1.3.2.tar.gz
.
File metadata
- Download URL: django-dynamic-filenames-1.3.2.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07c2f047fbb76c02f153305d53fdd95890cdbd5690072c7109a5638d4b456989 |
|
MD5 | 76a630cbba67a2392a29d99398499c9d |
|
BLAKE2b-256 | 6621983489a524a506e360336fd0d1846edd404b10ac3b89192009256c569563 |
Provenance
File details
Details for the file django_dynamic_filenames-1.3.2-py2.py3-none-any.whl
.
File metadata
- Download URL: django_dynamic_filenames-1.3.2-py2.py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a78cb1a7b3642dc57c14ef8cc345894d988a4453ca6125292cefd3c328450417 |
|
MD5 | 7718bd92cfa662d6bc03862baa8b087f |
|
BLAKE2b-256 | 59de9904ac45bd2b3a62a6bc4853bf8896c1383c07c248676538b9e65ab6a4d9 |