From 4930ef71f791e6982b1fbc60b0beb9bd83a5e6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Wed, 27 Aug 2025 00:38:41 +0800 Subject: [PATCH] midyf model_id --- CODE_OF_CONDUCT.md | 6 +----- apps/knowledge/serializers/document.py | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 67b063a9..0ec90da9 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -121,8 +121,4 @@ https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). -[homepage]: https://www.contributor-covenant.org - -For answers to common questions about this code of conduct, see the FAQ at -https://www.contributor-covenant.org/faq. Translations are available at -https://www.contributor-covenant.org/translations. \ No newline at end of file +[ diff --git a/apps/knowledge/serializers/document.py b/apps/knowledge/serializers/document.py index b06c0b3f..a5a69ca0 100644 --- a/apps/knowledge/serializers/document.py +++ b/apps/knowledge/serializers/document.py @@ -1180,6 +1180,7 @@ class DocumentSerializers(serializers.Serializer): # 插入文档 for document in instance_list: + maxkb_logger.info(f"Processing document: {document.get('name')}, paragraphs count: {len(document.get('paragraphs', []))}") document_paragraph_dict_model = DocumentSerializers.Create.get_document_paragraph_model( knowledge_id, document @@ -1188,7 +1189,9 @@ class DocumentSerializers(serializers.Serializer): document_instance = document_paragraph_dict_model.get('document') self.link_file(document.get('source_file_id'), document_instance.id) document_model_list.append(document_instance) - for paragraph in document_paragraph_dict_model.get('paragraph_model_list'): + para_list = document_paragraph_dict_model.get('paragraph_model_list') + maxkb_logger.info(f"Created {len(para_list)} paragraph models for document: {document_instance.id}") + for paragraph in para_list: paragraph_model_list.append(paragraph) for problem_paragraph_object in document_paragraph_dict_model.get('problem_paragraph_object_list'): problem_paragraph_object_list.append(problem_paragraph_object) @@ -1259,14 +1262,21 @@ class DocumentSerializers(serializers.Serializer): # 批量插入段落(只为非高级学习文档) if len(paragraph_model_list) > 0: + maxkb_logger.info(f"Total paragraphs to insert: {len(paragraph_model_list)}") for document in document_model_list: max_position = Paragraph.objects.filter(document_id=document.id).aggregate( max_position=Max('position') )['max_position'] or 0 - sub_list = [p for p in paragraph_model_list if p.document_id == document.id] + # 修复比较逻辑:确保类型一致的比较 + sub_list = [p for p in paragraph_model_list if str(p.document_id) == str(document.id)] + maxkb_logger.info(f"Document {document.id} will have {len(sub_list)} paragraphs") for i, paragraph in enumerate(sub_list): paragraph.position = max_position + i + 1 - QuerySet(Paragraph).bulk_create(sub_list if len(sub_list) > 0 else []) + if len(sub_list) > 0: + QuerySet(Paragraph).bulk_create(sub_list) + maxkb_logger.info(f"Successfully created {len(sub_list)} paragraphs for document {document.id}") + else: + maxkb_logger.warning(f"No paragraphs to create for document {document.id}") # 批量插入问题 bulk_create_in_batches(Problem, problem_model_list, batch_size=1000) # 批量插入关联问题