@@ -611,16 +611,15 @@ def get_through_model(self, field, model=None):
611611 and field .custom_object_type .id == custom_object_type_id
612612 )
613613
614+ # Use the actual model if provided, otherwise use string reference
615+ source_model = model if model else "netbox_custom_objects.CustomObject"
616+
614617 attrs = {
615618 "__module__" : "netbox_custom_objects.models" ,
616619 "Meta" : meta ,
617620 "id" : models .AutoField (primary_key = True ),
618621 "source" : models .ForeignKey (
619- (
620- "self"
621- if is_self_referential
622- else (model or "netbox_custom_objects.CustomObject" )
623- ),
622+ source_model ,
624623 on_delete = models .CASCADE ,
625624 related_name = "+" ,
626625 db_column = "source_id" ,
@@ -652,6 +651,8 @@ def get_model_field(self, field, **kwargs):
652651 and field .custom_object_type .id == custom_object_type_id
653652 )
654653
654+ # For now, we'll create the through model with string references
655+ # and resolve them later in after_model_generation
655656 through = self .get_through_model (field )
656657
657658 # For self-referential fields, use 'self' as the target
@@ -694,8 +695,14 @@ def after_model_generation(self, instance, model, field_name):
694695 if getattr (field , "_is_self_referential" , False ):
695696 field .remote_field .model = model
696697 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
699706 return
700707
701708 content_type = ContentType .objects .get (pk = instance .related_object_type_id )
@@ -713,12 +720,19 @@ def after_model_generation(self, instance, model, field_name):
713720 to_ct = f"{ content_type .app_label } .{ content_type .model } "
714721 to_model = apps .get_model (to_ct )
715722
716- # Update the M2M field 's model references
723+ # Update through model 's fields
717724 field .remote_field .model = to_model
718725
719726 # Update through model's target field
720727 through_model = field .remote_field .through
728+ source_field = through_model ._meta .get_field ("source" )
721729 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
722736 target_field .remote_field .model = to_model
723737 target_field .related_model = to_model
724738
@@ -751,15 +765,24 @@ def create_m2m_table(self, instance, model, field_name):
751765
752766 # Create the through model with actual model references
753767 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
756782
757783 # Register the model with Django's app registry
758784 apps = model ._meta .apps
759785
760- # if app_label is None:
761- # app_label = str(uuid.uuid4()) + "_database_table"
762- # apps = AppsProxy(dynamic_models=None, app_label=app_label)
763786 try :
764787 through_model = apps .get_model (APP_LABEL , instance .through_model_name )
765788 except LookupError :
@@ -769,6 +792,7 @@ def create_m2m_table(self, instance, model, field_name):
769792 # Update the M2M field's through model and target model
770793 field .remote_field .through = through_model
771794 field .remote_field .model = to_model
795+ field .remote_field .field_name = to_model ._meta .pk .name
772796
773797 # Create the through table
774798 with connection .schema_editor () as schema_editor :
0 commit comments