@@ -479,7 +479,9 @@ def _create_collection(self, model):
479479 else :
480480 encrypted_fields = encrypted_fields_map .get (db_table )
481481
482- if encrypted_fields and encrypted_fields .get ("fields" ):
482+ # if encrypted_fields and encrypted_fields.get("fields"):
483+
484+ if encrypted_fields :
483485 db .create_collection (db_table , encryptedFields = encrypted_fields )
484486 else :
485487 db .create_collection (db_table )
@@ -489,20 +491,20 @@ def _create_collection(self, model):
489491 db .create_collection (db_table )
490492
491493 def _get_encrypted_fields (
492- self , model , create_data_keys = False , key_alt_name = None , parent_model = None
494+ self , model , create_data_keys = False , key_alt_name = None , path_prefix = None
493495 ):
494496 """
495497 Recursively collect encryption schema data for only encrypted fields in a model.
496498 Returns None if no encrypted fields are found anywhere in the model hierarchy.
497499
498500 key_alt_name is the base path used for keyAltNames.
499- parent_model is the dot-notated path inside the document for schema mapping.
501+ path_prefix is the dot-notated path inside the document for schema mapping.
500502 """
501503 connection = self .connection
502504 client = connection .connection
503505 fields = model ._meta .fields
504506 key_alt_name = key_alt_name or model ._meta .db_table
505- parent_model = parent_model or ""
507+ path_prefix = path_prefix or ""
506508
507509 options = client ._options
508510 auto_encryption_opts = getattr (options , "auto_encryption_opts" , None )
@@ -520,7 +522,7 @@ def _get_encrypted_fields(
520522
521523 for field in fields :
522524 new_key_alt_name = f"{ key_alt_name } .{ field .column } "
523- new_parent_model = f"{ parent_model } .{ field .column } " if parent_model else field .column
525+ path = f"{ path_prefix } .{ field .column } " if path_prefix else field .column
524526
525527 # --- EmbeddedModelField ---
526528 if isinstance (field , EmbeddedModelField ):
@@ -550,7 +552,7 @@ def _get_encrypted_fields(
550552
551553 field_dict = {
552554 "bsonType" : "object" ,
553- "path" : new_parent_model ,
555+ "path" : path ,
554556 "keyId" : data_key ,
555557 }
556558 if getattr (field , "queries" , False ):
@@ -563,7 +565,7 @@ def _get_encrypted_fields(
563565 field .embedded_model ,
564566 create_data_keys = create_data_keys ,
565567 key_alt_name = new_key_alt_name ,
566- parent_model = new_parent_model ,
568+ path_prefix = path ,
567569 )
568570 if embedded_result and embedded_result .get ("fields" ):
569571 field_list .extend (embedded_result ["fields" ])
@@ -595,7 +597,7 @@ def _get_encrypted_fields(
595597
596598 field_dict = {
597599 "bsonType" : field .db_type (connection ),
598- "path" : new_parent_model ,
600+ "path" : path ,
599601 "keyId" : data_key ,
600602 }
601603 if getattr (field , "queries" , False ):
0 commit comments