From 58423ceb626e173f5cbdfa7d90b9d532d19438e2 Mon Sep 17 00:00:00 2001 From: stumpylog <797416+stumpylog@users.noreply.github.com> Date: Mon, 4 May 2026 14:12:26 -0700 Subject: [PATCH] Excludes the consumer and AnonymousUser from any models which might have a FK relation to it. This prevents orphan things like UI setting, which have a relation to no existing user --- .../management/commands/document_exporter.py | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/documents/management/commands/document_exporter.py b/src/documents/management/commands/document_exporter.py index efb19d587..b89faf7d9 100644 --- a/src/documents/management/commands/document_exporter.py +++ b/src/documents/management/commands/document_exporter.py @@ -365,6 +365,7 @@ class Command(CryptMixin, PaperlessCommand): # 2. Create manifest, containing all correspondents, types, tags, storage paths # note, documents and ui_settings + _excluded_usernames = ["consumer", "AnonymousUser"] manifest_key_to_object_query: dict[str, QuerySet[Any]] = { "correspondents": Correspondent.objects.all(), "tags": Tag.objects.all(), @@ -376,12 +377,16 @@ class Command(CryptMixin, PaperlessCommand): "saved_view_filter_rules": SavedViewFilterRule.objects.all(), "groups": Group.objects.all(), "users": User.objects.exclude( - username__in=["consumer", "AnonymousUser"], + username__in=_excluded_usernames, ).all(), - "ui_settings": UiSettings.objects.all(), + "ui_settings": UiSettings.objects.exclude( + user__username__in=_excluded_usernames, + ), "content_types": ContentType.objects.all(), "permissions": Permission.objects.all(), - "user_object_permissions": UserObjectPermission.objects.all(), + "user_object_permissions": UserObjectPermission.objects.exclude( + user__username__in=_excluded_usernames, + ), "group_object_permissions": GroupObjectPermission.objects.all(), "workflow_triggers": WorkflowTrigger.objects.all(), "workflow_actions": WorkflowAction.objects.all(), @@ -395,10 +400,16 @@ class Command(CryptMixin, PaperlessCommand): "documents": Document.global_objects.order_by("id").all(), "share_links": ShareLink.global_objects.all(), "share_link_bundles": ShareLinkBundle.objects.order_by("id").all(), - "social_accounts": SocialAccount.objects.all(), + "social_accounts": SocialAccount.objects.exclude( + user__username__in=_excluded_usernames, + ), "social_apps": SocialApp.objects.all(), - "social_tokens": SocialToken.objects.all(), - "authenticators": Authenticator.objects.all(), + "social_tokens": SocialToken.objects.exclude( + account__user__username__in=_excluded_usernames, + ), + "authenticators": Authenticator.objects.exclude( + user__username__in=_excluded_usernames, + ), } if settings.AUDIT_LOG_ENABLED: