Skip to main content

Write advanced filename patterns using the Format String Syntax.

Project description

Django DynFN: Advanced filename patterns using f-Strings
Documentation | Issues | Changelog | Funding 💚

Django Dynamic Filenames

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}/{instance.created:%Y-%m-%d}/{uuid:base32}{ext}"
)


class FileModel(models.Model):
    my_file = models.FileField(upload_to=upload_to_pattern)
    created = models.DateTimeField(auto_now_add=True)

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.

[!WARNING] Not all file systems support Base64 file names.

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


Download files

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

Source Distribution

django_dynamic_filenames-1.4.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

django_dynamic_filenames-1.4.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file django_dynamic_filenames-1.4.1.tar.gz.

File metadata

  • Download URL: django_dynamic_filenames-1.4.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_dynamic_filenames-1.4.1.tar.gz
Algorithm Hash digest
SHA256 63198da7b39aa51577f46932adf88b5836b3aa4e81721ef878f3c8bc8095ad1c
MD5 2056bf7fedb5c86fe7665a8721b43930
BLAKE2b-256 701a4e984920609bf0d867e1e1a9fb71afdaebcc9efc28ad07be3f8449a0680f

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_dynamic_filenames-1.4.1.tar.gz:

Publisher: release.yml on codingjoe/django-dynamic-filenames

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_dynamic_filenames-1.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_dynamic_filenames-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 785691f54fd772cd3ce71d65fb16b0d7df5c9157d2e19ef91a50ee5299c7d5f4
MD5 666c4e6debd7af5c5125d96a2a220158
BLAKE2b-256 a072d76e45bf0208dbfa7604dede12737a048f3ac2aa71bae2b4b793a802833b

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_dynamic_filenames-1.4.1-py3-none-any.whl:

Publisher: release.yml on codingjoe/django-dynamic-filenames

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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