Enhance import preview and backend handling for M2M relationships, adding sources to schema and improving missing values mapping for better template rendering and debugging.

This commit is contained in:
Ross
2025-10-20 12:01:07 +01:00
parent 88713aba5b
commit bddccd5768
2 changed files with 98 additions and 71 deletions
@@ -128,6 +128,14 @@
{% endif %} {% endif %}
</div> </div>
</div> </div>
<!-- DEBUG: show raw resolved and missing maps for troubleshooting -->
<div class="card-footer text-monospace small">
<details>
<summary>Debug: resolved_m2m / missing_map (click to expand)</summary>
<pre style="white-space:pre-wrap;">resolved_m2m: {{ item.resolved_m2m|default:"{}" }}
missing_map: {{ item.missing_map|default:"{}" }}</pre>
</details>
</div>
<div> <div>
<label><input type="checkbox" name="selected" value="{{ item.index }}" checked> Include</label> <label><input type="checkbox" name="selected" value="{{ item.index }}" checked> Include</label>
</div> </div>
+20 -1
View File
@@ -535,6 +535,7 @@ def import_llm_questions(request):
"condition": {"type": "array", "items": {"oneOf": [{"type": "integer"}, {"type": "string"}]}, "uniqueItems": True}, "condition": {"type": "array", "items": {"oneOf": [{"type": "integer"}, {"type": "string"}]}, "uniqueItems": True},
"presentation": {"type": "array", "items": {"oneOf": [{"type": "integer"}, {"type": "string"}]}, "uniqueItems": True}, "presentation": {"type": "array", "items": {"oneOf": [{"type": "integer"}, {"type": "string"}]}, "uniqueItems": True},
"subspecialty": {"type": "array", "items": {"oneOf": [{"type": "integer"}, {"type": "string"}]}, "uniqueItems": True}, "subspecialty": {"type": "array", "items": {"oneOf": [{"type": "integer"}, {"type": "string"}]}, "uniqueItems": True},
"sources": {"type": "array", "items": {"type": "string"}},
}, },
} }
@@ -685,11 +686,29 @@ def import_llm_questions(request):
m2m_map[key] = {"resolved": resolved, "missing": missing} m2m_map[key] = {"resolved": resolved, "missing": missing}
item_report["resolved_m2m"][key] = [o.name for o in resolved] item_report["resolved_m2m"][key] = [o.name for o in resolved]
# Always include an entry for this M2M (values may be empty)
item_report["missing_m2m"].append({"model": model_cls.__name__, "values": missing})
if missing: if missing:
item_report["missing_m2m"].extend([{"model": model_cls.__name__, "values": missing}])
for v in missing: for v in missing:
aggregated_would_create[model_cls.__name__].add(v) aggregated_would_create[model_cls.__name__].add(v)
# Build a simple map of missing values per model (stringified) for robust template rendering
# Provide both lowercase and capitalized keys so templates referencing either form will work
missing_map = {}
for k, v in m2m_map.items():
vals = [str(x) for x in v["missing"]]
missing_map[k] = vals
missing_map[k.capitalize()] = vals
item_report["missing_map"] = missing_map
# Debug: log missing_map and resolved_m2m for troubleshooting
logger.debug(
"import_llm_questions: item %s missing_map_keys=%s resolved_keys=%s",
idx,
list(missing_map.keys()),
list((item_report.get("resolved_m2m") or {}).keys()),
)
# If dry-run, do not save; just report what would happen # If dry-run, do not save; just report what would happen
if dry_run: if dry_run:
item_report["status"] = "would_create" item_report["status"] = "would_create"