Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
django-dynamic-filenames
Advanced tools
Write advanced filename patterns using the Format String Syntax.
Write advanced filename patterns using the Format String Syntax.
pip install django-dynamic-filenames
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:
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
::: title
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.
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.
FAQs
Write advanced filename patterns using the Format String Syntax.
We found that django-dynamic-filenames demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.