Skip to content

Commit bb4e14e

Browse files
committed
Get model_string from field to avoid using "netbox_custom_objects.CustomObject" as a placeholder
1 parent 33e8aea commit bb4e14e

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

netbox_custom_objects/field_types.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ def get_through_model(self, field, model=None):
702702
)
703703

704704
# Use the actual model if provided, otherwise use string reference
705-
source_model = model if model else "netbox_custom_objects.CustomObject"
705+
source_model = model
706706

707707
attrs = {
708708
"__module__": "netbox_custom_objects.models",
@@ -715,7 +715,7 @@ def get_through_model(self, field, model=None):
715715
db_column="source_id",
716716
),
717717
"target": models.ForeignKey(
718-
"self" if is_self_referential else "netbox_custom_objects.CustomObject",
718+
"self" if is_self_referential else model,
719719
on_delete=models.CASCADE,
720720
related_name="+",
721721
db_column="target_id",
@@ -745,11 +745,13 @@ def get_model_field(self, field, **kwargs):
745745

746746
# For now, we'll create the through model with string references
747747
# and resolve them later in after_model_generation
748-
through = self.get_through_model(field)
748+
# TODO: Check whether later resolution of the model is actually necessary or can be passed as string
749+
model_string = f"{field.related_object_type.app_label}.{field.related_object_type.model}"
750+
through = self.get_through_model(field, model_string)
749751

750752
# For self-referential fields, use 'self' as the target
751753
m2m_field = CustomManyToManyField(
752-
to="self" if is_self_referential else "netbox_custom_objects.CustomObject",
754+
to="self" if is_self_referential else model_string,
753755
through=through,
754756
through_fields=("source", "target"),
755757
blank=True,

0 commit comments

Comments
 (0)