Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Add 'doc_pk' to PAPERLESS_FILENAME_FORMAT handling (#3861)

mechanarchy6ad3d45d6016
diff --git a/docs/advanced_usage.md b/docs/advanced_usage.md
index 199931bb5..89530db7f 100644
--- a/docs/advanced_usage.md
+++ b/docs/advanced_usage.md
@@ -311,6 +311,7 @@ Paperless provides the following placeholders within filenames:
- `{added_day}`: Day added only (number 01-31).
- `{owner_username}`: Username of document owner, if any, or "none"
- `{original_name}`: Document original filename, minus the extension, if any, or "none"
+- `{doc_pk}`: The paperless identifier (primary key) for the document.
Paperless will try to conserve the information from your database as
much as possible. However, some characters that you can use in document
diff --git a/src/documents/file_handling.py b/src/documents/file_handling.py
index e382d4965..d9601ccd9 100644
--- a/src/documents/file_handling.py
+++ b/src/documents/file_handling.py
@@ -218,6 +218,7 @@ def generate_filename(
tag_list=tag_list,
owner_username=owner_username_str,
original_name=original_name,
+ doc_pk=f"{doc.pk:07}",
).strip()
if settings.FILENAME_FORMAT_REMOVE_NONE:
diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py
index b021f8aaf..9e27b9aad 100644
--- a/src/documents/tests/test_file_handling.py
+++ b/src/documents/tests/test_file_handling.py
@@ -446,6 +446,19 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertIsNotDir(os.path.join(settings.ORIGINALS_DIR, "none"))
self.assertIsDir(settings.ORIGINALS_DIR)
+ @override_settings(FILENAME_FORMAT="{doc_pk}")
+ def test_format_doc_pk(self):
+ document = Document()
+ document.pk = 1
+ document.mime_type = "application/pdf"
+ document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
+
+ self.assertEqual(generate_filename(document), "0000001.pdf")
+
+ document.pk = 13579
+
+ self.assertEqual(generate_filename(document), "0013579.pdf")
+
@override_settings(FILENAME_FORMAT=None)
def test_format_none(self):
document = Document()