# Django Dynamic Filenames
Write advanced filename patterns using the [Format String
Syntax](https://docs.python.org/3/library/string.html#format-string-syntax).
## Getting Started
### Installation
``` bash
pip install django-dynamic-filenames
```
### Samples
Basic example:
``` python
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.
:::: 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.
### Type specifiers
You can also use a special slug type specifier, that slugifies strings.
Example:
``` python
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.
Raw data
{
"_id": null,
"home_page": null,
"name": "django-dynamic-filenames",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "django, django-storages, file, filenames, django-pictures",
"author": null,
"author_email": "Johannes Maron <johannes@maron.family>",
"download_url": "https://files.pythonhosted.org/packages/3f/d6/a895456f0fc9e1ca3e7772d4aebf9200815226ea738508023de1f7c23629/django_dynamic_filenames-1.4.0.tar.gz",
"platform": null,
"description": "# Django Dynamic Filenames\n\nWrite advanced filename patterns using the [Format String\nSyntax](https://docs.python.org/3/library/string.html#format-string-syntax).\n\n## Getting Started\n\n### Installation\n\n``` bash\npip install django-dynamic-filenames\n```\n\n### Samples\n\nBasic example:\n\n``` python\nfrom django.db import models\nfrom dynamic_filenames import FilePattern\n\nupload_to_pattern = FilePattern(\n filename_pattern='{app_label:.25}/{model_name:.30}/{uuid:base32}{ext}'\n)\n\nclass FileModel(models.Model):\n my_file = models.FileField(upload_to=upload_to_pattern)\n```\n\nAuto slug example:\n\n## Features\n\n### Field names\n\n`ext`\n\n: File extension including the dot.\n\n`name`\n\n: Filename excluding the folders.\n\n`model_name`\n\n: Name of the Django model.\n\n`app_label`\n\n: App label of the Django model.\n\n`instance`\n\n: Instance of the model before it has been saved. You may not have a\n primary key at this point.\n\n`uuid`\n\n: UUID version 4 that supports multiple type specifiers. The UUID will\n be the same should you use it twice in the same string, but\n different on each invocation of the `upload_to` callable.\n\n The type specifiers allow you to format the UUID in different ways,\n e.g. `{uuid:x}` will give you a with a hexadecimal UUID.\n\n The supported type specifiers are:\n\n `s`\n\n : String representation of a UUID including dashes.\n\n `i`\n\n : Integer representation of a UUID. Like to `UUID.int`.\n\n `x`\n\n : Hexadecimal (Base16) representation of a UUID. Like to\n `UUID.hex`.\n\n `X`\n\n : Upper case hexadecimal representation of a UUID. Like to\n `UUID.hex`.\n\n `base32`\n\n : Base32 representation of a UUID without padding.\n\n `base64`\n\n : Base64 representation of a UUID without padding.\n\n :::: warning\n ::: title\n Warning\n :::\n\n Not all file systems support Base64 file names.\n ::::\n\n All type specifiers also support precisions to cut the string, e.g.\n `{{uuid:.2base32}}` would only return the first 2 characters of a\n Base32 encoded UUID.\n\n### Type specifiers\n\nYou can also use a special slug type specifier, that slugifies strings.\n\nExample:\n\n``` python\nfrom django.db import models\nfrom dynamic_filenames import FilePattern\n\nupload_to_pattern = FilePattern(\n filename_pattern='{app_label:.25}/{model_name:.30}/{instance.title:.40slug}{ext}'\n)\n\nclass FileModel(models.Model):\n title = models.CharField(max_length=100)\n my_file = models.FileField(upload_to=upload_to_pattern)\n```\n\nSlug type specifiers also support precisions to cut the string. In the\nexample above the slug of the instance title will be cut at 40\ncharacters.\n\n",
"bugtrack_url": null,
"license": null,
"summary": "Write advanced filename patterns using the Format String Syntax.",
"version": "1.4.0",
"project_urls": {
"Changelog": "https://github.com/codingjoe/django-dynamic-filenames/releases",
"Documentation": "https://github.com/codingjoe/django-dynamic-filenames#django-dynamic-filenames",
"Issue-Tracker": "https://github.com/codingjoe/django-dynamic-filenames/issues",
"Project-URL": "https://github.com/codingjoe/django-dynamic-filenames",
"Source": "https://github.com/codingjoe/django-dynamic-filenames"
},
"split_keywords": [
"django",
" django-storages",
" file",
" filenames",
" django-pictures"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "24d8982ed84bba190c4709129594a1d5ae600cec43d962da6bbdce73775208f8",
"md5": "cfb9e9b94392736b92aec1c8ec882ae5",
"sha256": "afbda639b0e54ab3ed7dfed60ed9b388143c07c8cc3b7a33f9904a9696d9de83"
},
"downloads": -1,
"filename": "django_dynamic_filenames-1.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cfb9e9b94392736b92aec1c8ec882ae5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 5305,
"upload_time": "2024-10-09T09:08:49",
"upload_time_iso_8601": "2024-10-09T09:08:49.461495Z",
"url": "https://files.pythonhosted.org/packages/24/d8/982ed84bba190c4709129594a1d5ae600cec43d962da6bbdce73775208f8/django_dynamic_filenames-1.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3fd6a895456f0fc9e1ca3e7772d4aebf9200815226ea738508023de1f7c23629",
"md5": "71362802785d0b1061b06da15602393f",
"sha256": "8913151c2eb505079dd1dd6891e20150806a3a59fdd71a61436d5af4a0fd7b8c"
},
"downloads": -1,
"filename": "django_dynamic_filenames-1.4.0.tar.gz",
"has_sig": false,
"md5_digest": "71362802785d0b1061b06da15602393f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 4642,
"upload_time": "2024-10-09T09:08:51",
"upload_time_iso_8601": "2024-10-09T09:08:51.421228Z",
"url": "https://files.pythonhosted.org/packages/3f/d6/a895456f0fc9e1ca3e7772d4aebf9200815226ea738508023de1f7c23629/django_dynamic_filenames-1.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-09 09:08:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "codingjoe",
"github_project": "django-dynamic-filenames",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "django-dynamic-filenames"
}