@@ -611,16 +611,15 @@ def get_through_model(self, field, model=None):
611
611
and field .custom_object_type .id == custom_object_type_id
612
612
)
613
613
614
+ # Use the actual model if provided, otherwise use string reference
615
+ source_model = model if model else "netbox_custom_objects.CustomObject"
616
+
614
617
attrs = {
615
618
"__module__" : "netbox_custom_objects.models" ,
616
619
"Meta" : meta ,
617
620
"id" : models .AutoField (primary_key = True ),
618
621
"source" : models .ForeignKey (
619
- (
620
- "self"
621
- if is_self_referential
622
- else (model or "netbox_custom_objects.CustomObject" )
623
- ),
622
+ source_model ,
624
623
on_delete = models .CASCADE ,
625
624
related_name = "+" ,
626
625
db_column = "source_id" ,
@@ -652,6 +651,8 @@ def get_model_field(self, field, **kwargs):
652
651
and field .custom_object_type .id == custom_object_type_id
653
652
)
654
653
654
+ # For now, we'll create the through model with string references
655
+ # and resolve them later in after_model_generation
655
656
through = self .get_through_model (field )
656
657
657
658
# For self-referential fields, use 'self' as the target
@@ -694,8 +695,14 @@ def after_model_generation(self, instance, model, field_name):
694
695
if getattr (field , "_is_self_referential" , False ):
695
696
field .remote_field .model = model
696
697
through_model = field .remote_field .through
697
- through_model ._meta .get_field ("target" ).remote_field .model = model
698
- through_model ._meta .get_field ("target" ).related_model = model
698
+
699
+ # Update both source and target fields to point to the same model
700
+ source_field = through_model ._meta .get_field ("source" )
701
+ target_field = through_model ._meta .get_field ("target" )
702
+ source_field .remote_field .model = model
703
+ source_field .related_model = model
704
+ target_field .remote_field .model = model
705
+ target_field .related_model = model
699
706
return
700
707
701
708
content_type = ContentType .objects .get (pk = instance .related_object_type_id )
@@ -713,12 +720,19 @@ def after_model_generation(self, instance, model, field_name):
713
720
to_ct = f"{ content_type .app_label } .{ content_type .model } "
714
721
to_model = apps .get_model (to_ct )
715
722
716
- # Update the M2M field 's model references
723
+ # Update through model 's fields
717
724
field .remote_field .model = to_model
718
725
719
726
# Update through model's target field
720
727
through_model = field .remote_field .through
728
+ source_field = through_model ._meta .get_field ("source" )
721
729
target_field = through_model ._meta .get_field ("target" )
730
+
731
+ # Source field should point to the current model
732
+ source_field .remote_field .model = model
733
+ source_field .related_model = model
734
+
735
+ # Target field should point to the related model
722
736
target_field .remote_field .model = to_model
723
737
target_field .related_model = to_model
724
738
@@ -751,15 +765,24 @@ def create_m2m_table(self, instance, model, field_name):
751
765
752
766
# Create the through model with actual model references
753
767
through = self .get_through_model (instance , model )
754
- through ._meta .get_field ("target" ).remote_field .model = to_model
755
- through ._meta .get_field ("target" ).related_model = to_model
768
+
769
+ # Update the through model's foreign key references
770
+ source_field = through ._meta .get_field ("source" )
771
+ target_field = through ._meta .get_field ("target" )
772
+
773
+ # Source field should point to the current model
774
+ source_field .remote_field .model = model
775
+ source_field .remote_field .field_name = model ._meta .pk .name
776
+ source_field .related_model = model
777
+
778
+ # Target field should point to the related model
779
+ target_field .remote_field .model = to_model
780
+ target_field .remote_field .field_name = to_model ._meta .pk .name
781
+ target_field .related_model = to_model
756
782
757
783
# Register the model with Django's app registry
758
784
apps = model ._meta .apps
759
785
760
- # if app_label is None:
761
- # app_label = str(uuid.uuid4()) + "_database_table"
762
- # apps = AppsProxy(dynamic_models=None, app_label=app_label)
763
786
try :
764
787
through_model = apps .get_model (APP_LABEL , instance .through_model_name )
765
788
except LookupError :
@@ -769,6 +792,7 @@ def create_m2m_table(self, instance, model, field_name):
769
792
# Update the M2M field's through model and target model
770
793
field .remote_field .through = through_model
771
794
field .remote_field .model = to_model
795
+ field .remote_field .field_name = to_model ._meta .pk .name
772
796
773
797
# Create the through table
774
798
with connection .schema_editor () as schema_editor :
0 commit comments