|
| 1 | +# Django Dynamic Filenames |
| 2 | + |
| 3 | +Write advanced filename patterns using the [Format String |
| 4 | +Syntax](https://docs.python.org/3/library/string.html#format-string-syntax). |
| 5 | + |
| 6 | +## Getting Started |
| 7 | + |
| 8 | +### Installation |
| 9 | + |
| 10 | +``` sourceCode bash |
| 11 | +pip install django-dynamic-filenames |
| 12 | +``` |
| 13 | + |
| 14 | +### Samples |
| 15 | + |
| 16 | +Basic example: |
| 17 | + |
| 18 | +``` sourceCode python |
| 19 | +from django.db import models |
| 20 | +from dynamic_names import FilePattern |
| 21 | +
|
| 22 | +upload_to_pattern = FilePattern( |
| 23 | + filename_pattern='{app_name:.25}/{model_name:.30}/{uuid:base32}{ext}' |
| 24 | +) |
| 25 | +
|
| 26 | +class FileModel(models.Model): |
| 27 | + my_file = models.FileField(upload_to=upload_to_pattern) |
| 28 | +``` |
| 29 | + |
| 30 | +Auto slug example: |
| 31 | + |
| 32 | +## Features |
| 33 | + |
| 34 | +### Field names |
| 35 | + |
| 36 | + - `ext` |
| 37 | + File extension including the dot. |
| 38 | + |
| 39 | + - `name` |
| 40 | + Filename excluding the folders. |
| 41 | + |
| 42 | + - `model_name` |
| 43 | + Name of the Django model. |
| 44 | + |
| 45 | + - `app_label` |
| 46 | + App label of the Django model. |
| 47 | + |
| 48 | + - `instance` |
| 49 | + Instance of the model before it has been saved. You may not have a |
| 50 | + primary key at this point. |
| 51 | + |
| 52 | + - `uuid` |
| 53 | + UUID version 4 that supports multiple type specifiers. The UUID will |
| 54 | + be the same should you use it twice in the same string, but |
| 55 | + different on each invocation of the `upload_to` callable. |
| 56 | + |
| 57 | + The type specifiers allow you to format the UUID in different ways, |
| 58 | + e.g. `{uuid:x}` will give you a with a hexadecimal UUID. |
| 59 | + |
| 60 | + The supported type specifiers are: |
| 61 | + |
| 62 | + - `s` |
| 63 | + String representation of a UUID including dashes. |
| 64 | + |
| 65 | + - `i` |
| 66 | + Integer representation of a UUID. Like to `UUID.int`. |
| 67 | + |
| 68 | + - `x` |
| 69 | + Hexadecimal (Base16) representation of a UUID. Like to |
| 70 | + `UUID.hex`. |
| 71 | + |
| 72 | + - `X` |
| 73 | + Upper case hexadecimal representation of a UUID. Like to |
| 74 | + `UUID.hex`. |
| 75 | + |
| 76 | + - `base32` |
| 77 | + Base32 representation of a UUID without padding. |
| 78 | + |
| 79 | + - `base64` |
| 80 | + Base64 representation of a UUID without padding. |
| 81 | + |
| 82 | + **Warning:** Not all file systems support Base64 file names. |
| 83 | + |
| 84 | + All type specifiers also support precisions to cut the string, e.g. |
| 85 | + `{{uuid:.2base32}}` will return the first 2 characters of a Base32 |
| 86 | + encoded UUID. |
| 87 | + |
| 88 | +### Type specifiers |
| 89 | + |
| 90 | +You can also use a special slug type specifier, that slugifies strings. |
| 91 | + |
| 92 | +Example: |
| 93 | + |
| 94 | +``` sourceCode python |
| 95 | +from django.db import models |
| 96 | +from dynamic_names import FilePattern |
| 97 | +
|
| 98 | +upload_to_pattern = FilePattern( |
| 99 | + filename_pattern='{app_name:.25}/{model_name:.30}/{instance.title:.40slug}{ext}' |
| 100 | +) |
| 101 | +
|
| 102 | +class FileModel(models.Model): |
| 103 | + title = models.CharField(max_length=100) |
| 104 | + my_file = models.FileField(upload_to=upload_to_pattern) |
| 105 | +``` |
| 106 | + |
| 107 | +Slug type specifiers also support precisions to cut the string. In the |
| 108 | +example above the slug of the instance title will be cut at 40 |
| 109 | +characters. |
0 commit comments