Fix: defer add_nested_tags in ai workflow to avoid losing unsaved changes

This commit is contained in:
shamoon
2026-08-26 14:18:40 -07:00
parent 808746c28c
commit 406bc1a233
2 changed files with 42 additions and 3 deletions
+7 -3
View File
@@ -226,20 +226,20 @@ def apply_ai_suggestions_to_document(
document.created = created
updated_fields.append("created")
tags_to_add: list[Tag] = []
if AISuggestionField.TAGS in selected:
choice = suggestions["tags"]
names = choice["new_names"]
tags = resolve_tags(
tags_to_add = resolve_tags(
names,
resolve_tag_ids(choice["existing_ids"], owner)
+ match_tags_by_name(names, owner),
create_missing=create_missing,
owner=owner,
)
if tags:
if tags_to_add:
# Suggested tags are always added, so overwrite_existing
# does not really apply here
document.add_nested_tags(tags)
updated_fields.append("tags")
if updated_fields:
@@ -249,6 +249,10 @@ def apply_ai_suggestions_to_document(
]
document.save(update_fields=[*direct_updated_fields, "modified"])
# Tags at the end so m2m_changed doesn't trigger db and overwrite other changes
if tags_to_add:
document.add_nested_tags(tags_to_add)
logger.info(
"Applied AI suggestions %s to document %s",
updated_fields or "(none)",