@@ -395,7 +395,7 @@ class Query(serializers.Serializer):
395395 tag_ids = serializers .ListField (child = serializers .UUIDField (), allow_null = True , required = False ,
396396 allow_empty = True )
397397 no_tag = serializers .BooleanField (required = False , default = False , allow_null = True )
398- tag_exclude = serializers .BooleanField (required = False ,default = False , allow_null = True )
398+ tag_exclude = serializers .BooleanField (required = False , default = False , allow_null = True )
399399
400400 def get_query_set (self ):
401401 query_set = QuerySet (model = Document )
@@ -1350,17 +1350,19 @@ def batch_add_tag(self, instance: Dict, with_valid=True):
13501350 ).values_list ('document_id' , 'tag_id' )
13511351 }
13521352
1353- # 批量创建不存在的关联关系
1354- new_relations = [
1355- DocumentTag (
1356- id = uuid .uuid7 (),
1357- document_id = document_id ,
1358- tag_id = tag_id ,
1359- )
1360- for document_id in document_id_list
1361- for tag_id in tag_id_list
1362- if (document_id , tag_id ) not in existing_relations
1363- ]
1353+ new_relations = []
1354+ for doc_id in document_id_list :
1355+ for tag_id in tag_id_list :
1356+ relation_key = (str (doc_id ), str (tag_id ))
1357+
1358+ # 既检查数据库中已存在的,也检查本次即将创建的
1359+ if relation_key not in existing_relations :
1360+ new_relations .append (DocumentTag (
1361+ id = uuid .uuid7 (),
1362+ document_id = doc_id ,
1363+ tag_id = tag_id ,
1364+ ))
1365+ existing_relations .add (relation_key )
13641366
13651367 if new_relations :
13661368 QuerySet (DocumentTag ).bulk_create (new_relations )
@@ -1629,21 +1631,22 @@ def is_valid(self, *, raise_exception=False):
16291631 ).exists ():
16301632 raise AppApiException (500 , _ ('Tag id does not exist' ))
16311633
1632- def batch_delete_docs_tag (self , instance ,with_valid = True ):
1634+ def batch_delete_docs_tag (self , instance , with_valid = True ):
16331635 if with_valid :
16341636 BatchSerializer (data = instance ).is_valid (model = Document , raise_exception = True )
16351637 self .is_valid (raise_exception = True )
16361638 knowledge_id = self .data .get ('knowledge_id' )
1637- tag_id = self .data .get ('tag_id' )
1639+ tag_id = self .data .get ('tag_id' )
16381640 doc_id_list = instance .get ("id_list" )
16391641
16401642 valid_doc_count = Document .objects .filter (id__in = doc_id_list , knowledge_id = knowledge_id ).count ()
16411643 if valid_doc_count != len (doc_id_list ):
16421644 raise AppApiException (500 , _ ('Document id does not belong to current knowledge' ))
16431645
1644- DocumentTag .objects .filter (document_id__in = doc_id_list ,tag_id = tag_id ).delete ()
1646+ DocumentTag .objects .filter (document_id__in = doc_id_list , tag_id = tag_id ).delete ()
16451647
16461648 return True
1649+
16471650 class ReplaceSourceFile (serializers .Serializer ):
16481651 workspace_id = serializers .CharField (required = True , label = _ ('workspace id' ))
16491652 knowledge_id = serializers .UUIDField (required = True , label = _ ('knowledge id' ))
0 commit comments