@@ -35,10 +35,10 @@ class LazyForeignKey(ForeignKey):
35
35
36
36
def __init__ (self , to_model_name , * args , ** kwargs ):
37
37
self ._to_model_name = to_model_name
38
-
38
+
39
39
# Filter out our custom parameters before passing to Django's ForeignKey
40
40
field_kwargs = {k : v for k , v in kwargs .items () if not k .startswith ('_' ) and k != 'generating_models' }
41
-
41
+
42
42
super ().__init__ (to_model_name , * args , ** field_kwargs )
43
43
44
44
def contribute_to_class (self , cls , name , ** kwargs ):
@@ -84,7 +84,7 @@ def _safe_kwargs(self, **kwargs):
84
84
Create a safe kwargs dict that can be passed to Django field constructors.
85
85
This method automatically filters out any custom parameters.
86
86
"""
87
- return {k : v for k , v in kwargs .items ()
87
+ return {k : v for k , v in kwargs .items ()
88
88
if not k .startswith ('_' ) and k != 'generating_models' }
89
89
90
90
def get_annotated_form_field (self , field , enforce_visibility = True , ** kwargs ):
@@ -93,7 +93,7 @@ def get_annotated_form_field(self, field, enforce_visibility=True, **kwargs):
93
93
form_field .label = str (field )
94
94
# Set the field name so Django can properly bind it to the instance
95
95
form_field .name = field .name
96
-
96
+
97
97
if field .description :
98
98
form_field .help_text = render_markdown (field .description )
99
99
@@ -387,7 +387,7 @@ class ObjectFieldType(FieldType):
387
387
def get_model_field (self , field , ** kwargs ):
388
388
content_type = ContentType .objects .get (pk = field .related_object_type_id )
389
389
to_model = content_type .model
390
-
390
+
391
391
# Extract our custom parameters and keep only Django field parameters
392
392
generating_models = kwargs .pop ('_generating_models' , getattr (self , '_generating_models' , set ()))
393
393
field_kwargs = {k : v for k , v in kwargs .items () if not k .startswith ('_' )}
@@ -401,16 +401,16 @@ def get_model_field(self, field, **kwargs):
401
401
"model" , ""
402
402
)
403
403
custom_object_type = CustomObjectType .objects .get (pk = custom_object_type_id )
404
-
404
+
405
405
# Check if this is a self-referential field
406
406
if custom_object_type .id == field .custom_object_type .id :
407
407
# For self-referential fields, use LazyForeignKey to defer resolution
408
408
model_name = f"{ APP_LABEL } .{ custom_object_type .get_table_model_name (custom_object_type .id )} "
409
409
f = LazyForeignKey (
410
410
model_name ,
411
- null = True ,
412
- blank = True ,
413
- on_delete = models .CASCADE ,
411
+ null = True ,
412
+ blank = True ,
413
+ on_delete = models .CASCADE ,
414
414
** field_kwargs
415
415
)
416
416
return f
@@ -423,9 +423,9 @@ def get_model_field(self, field, **kwargs):
423
423
model_name = f"{ APP_LABEL } .{ custom_object_type .get_table_model_name (custom_object_type .id )} "
424
424
f = models .ForeignKey (
425
425
model_name ,
426
- null = True ,
427
- blank = True ,
428
- on_delete = models .CASCADE ,
426
+ null = True ,
427
+ blank = True ,
428
+ on_delete = models .CASCADE ,
429
429
** field_kwargs
430
430
)
431
431
return f
@@ -435,11 +435,11 @@ def get_model_field(self, field, **kwargs):
435
435
# to_model = content_type.model_class()._meta.object_name
436
436
to_ct = f"{ content_type .app_label } .{ to_model } "
437
437
model = apps .get_model (to_ct )
438
-
438
+
439
439
f = models .ForeignKey (
440
440
model , null = True , blank = True , on_delete = models .CASCADE , ** field_kwargs
441
441
)
442
-
442
+
443
443
return f
444
444
445
445
def get_form_field (self , field , for_csv_import = False , ** kwargs ):
@@ -458,7 +458,7 @@ def get_form_field(self, field, for_csv_import=False, **kwargs):
458
458
"model" , ""
459
459
)
460
460
custom_object_type = CustomObjectType .objects .get (pk = custom_object_type_id )
461
-
461
+
462
462
# Check if we're in a recursion situation
463
463
generating_models = getattr (self , '_generating_models' , set ())
464
464
if generating_models and custom_object_type .id in generating_models :
@@ -737,7 +737,7 @@ def get_model_field(self, field, **kwargs):
737
737
custom_object_type_id = content_type .model .replace ("table" , "" ).replace (
738
738
"model" , ""
739
739
)
740
-
740
+
741
741
# Extract our custom parameters and keep only Django field parameters
742
742
generating_models = kwargs .pop ('_generating_models' , getattr (self , '_generating_models' , set ()))
743
743
field_kwargs = {k : v for k , v in kwargs .items () if not k .startswith ('_' )}
@@ -860,16 +860,16 @@ def after_model_generation(self, instance, model, field_name):
860
860
# Update both source and target fields to point to the same model
861
861
source_field = through_model ._meta .get_field ("source" )
862
862
target_field = through_model ._meta .get_field ("target" )
863
-
863
+
864
864
# Resolve the foreign key fields to point to the actual model
865
865
source_field .remote_field .model = model
866
866
source_field .related_model = model
867
867
target_field .remote_field .model = model
868
868
target_field .related_model = model
869
-
869
+
870
870
# Also update the field's to attribute to point to the actual model
871
871
field .to = model
872
-
872
+
873
873
return
874
874
875
875
# For non-self-referential fields, we need to resolve the target model
@@ -884,7 +884,7 @@ def after_model_generation(self, instance, model, field_name):
884
884
"model" , ""
885
885
)
886
886
custom_object_type = CustomObjectType .objects .get (pk = custom_object_type_id )
887
-
887
+
888
888
# For self-referential fields, we need to resolve them to the current model
889
889
# This doesn't cause recursion because we're not calling get_model() again
890
890
if custom_object_type .id == instance .custom_object_type .id :
@@ -941,7 +941,7 @@ def create_m2m_table(self, instance, model, field_name):
941
941
custom_object_type = CustomObjectType .objects .get (
942
942
pk = custom_object_type_id
943
943
)
944
-
944
+
945
945
# Check if we're in a recursion situation
946
946
generating_models = getattr (self , '_generating_models' , set ())
947
947
if generating_models and custom_object_type .id in generating_models :
0 commit comments