from django.contrib import admin from django import forms from .models import MindMap @admin.register(MindMap) class MindMapAdmin(admin.ModelAdmin): list_display = ("title", "created_at", "has_mindmap") search_fields = ("title",) prepopulated_fields = {"slug": ("title",)} readonly_fields = ("updated_at",) def has_mindmap(self, obj): return bool(obj.mindmap_data and obj.mindmap_data.get("data")) has_mindmap.boolean = True has_mindmap.short_description = "已解析" def save_model(self, request, obj, form, change): is_new = not obj.pk super().save_model(request, obj, form, change) if "xmind_file" in form.changed_data and obj.xmind_file: try: obj.parse_and_store(obj.xmind_file.path) self.message_user(request, f"脑图「{obj.title}」解析成功") except Exception as e: self.message_user( request, f"解析失败: {e}", level="error" )