Skip to content

Commit 17466f3

Browse files
committed
always use lazy model refs
1 parent 14d4743 commit 17466f3

File tree

1 file changed

+14
-40
lines changed

1 file changed

+14
-40
lines changed

netbox_custom_objects/field_types.py

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ def get_model_field(self, field, **kwargs):
396396
to_model = content_type.model
397397

398398
# Extract our custom parameters and keep only Django field parameters
399-
generating_models = kwargs.pop('_generating_models', getattr(self, '_generating_models', set()))
400399
field_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
401400
field_kwargs.update({"default": field.default, "unique": field.unique})
402401

@@ -409,45 +408,20 @@ def get_model_field(self, field, **kwargs):
409408
)
410409
custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id)
411410

412-
# Check if this is a self-referential field
413-
if custom_object_type.id == field.custom_object_type.id:
414-
# For self-referential fields, use LazyForeignKey to defer resolution
415-
model_name = f"{APP_LABEL}.{custom_object_type.get_table_model_name(custom_object_type.id)}"
416-
# Generate a unique related_name to prevent reverse accessor conflicts
417-
table_model_name = field.custom_object_type.get_table_model_name(field.custom_object_type.id).lower()
418-
related_name = f"{table_model_name}_{field.name}_set"
419-
f = LazyForeignKey(
420-
model_name,
421-
null=True,
422-
blank=True,
423-
on_delete=models.CASCADE,
424-
related_name=related_name,
425-
**field_kwargs
426-
)
427-
return f
428-
else:
429-
# For cross-referential fields, use skip_object_fields to avoid infinite loops
430-
# Check if we're in a recursion situation using the parameter or stored attribute
431-
if generating_models and custom_object_type.id in generating_models:
432-
# We're in a circular reference, don't call get_model() to prevent recursion
433-
# Use a string reference instead
434-
model_name = f"{APP_LABEL}.{custom_object_type.get_table_model_name(custom_object_type.id)}"
435-
# Generate a unique related_name to prevent reverse accessor conflicts
436-
table_model_name = field.custom_object_type.get_table_model_name(
437-
field.custom_object_type.id
438-
).lower()
439-
related_name = f"{table_model_name}_{field.name}_set"
440-
f = models.ForeignKey(
441-
model_name,
442-
null=True,
443-
blank=True,
444-
on_delete=models.CASCADE,
445-
related_name=related_name,
446-
**field_kwargs
447-
)
448-
return f
449-
else:
450-
model = custom_object_type.get_model(skip_object_fields=True)
411+
# For self-referential fields, use LazyForeignKey to defer resolution
412+
model_name = f"{APP_LABEL}.{custom_object_type.get_table_model_name(custom_object_type.id)}"
413+
# Generate a unique related_name to prevent reverse accessor conflicts
414+
table_model_name = field.custom_object_type.get_table_model_name(field.custom_object_type.id).lower()
415+
related_name = f"{table_model_name}_{field.name}_set"
416+
f = LazyForeignKey(
417+
model_name,
418+
null=True,
419+
blank=True,
420+
on_delete=models.CASCADE,
421+
related_name=related_name,
422+
**field_kwargs
423+
)
424+
return f
451425
else:
452426
# to_model = content_type.model_class()._meta.object_name
453427
to_ct = f"{content_type.app_label}.{to_model}"

0 commit comments

Comments
 (0)