|
28 | 28 | from . import field_types, filtersets, forms, tables |
29 | 29 | from .models import CustomObject, CustomObjectType, CustomObjectTypeField |
30 | 30 | from extras.choices import CustomFieldTypeChoices |
| 31 | +from netbox_custom_objects.constants import APP_LABEL |
31 | 32 |
|
32 | 33 | logger = logging.getLogger("netbox_custom_objects.views") |
33 | 34 |
|
@@ -493,13 +494,49 @@ def get_form(self, model): |
493 | 494 |
|
494 | 495 | # Create a custom __init__ method to set instance attributes |
495 | 496 | def custom_init(self, *args, **kwargs): |
496 | | - forms.NetBoxModelForm.__init__(self, *args, **kwargs) |
497 | 497 | # Set the grouping info as instance attributes from the outer scope |
498 | 498 | self.custom_object_type_fields = attrs["custom_object_type_fields"] |
499 | 499 | self.custom_object_type_field_groups = attrs[ |
500 | 500 | "custom_object_type_field_groups" |
501 | 501 | ] |
502 | 502 |
|
| 503 | + # Handle default values for MultiObject fields BEFORE calling parent __init__ |
| 504 | + # This ensures the initial values are set before Django processes the form |
| 505 | + instance = kwargs.get('instance', None) |
| 506 | + if not instance or not instance.pk: |
| 507 | + # Only set defaults for new instances (not when editing existing ones) |
| 508 | + for field_name, field_obj in self.custom_object_type_fields.items(): |
| 509 | + if field_obj.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT: |
| 510 | + if field_obj.default and isinstance(field_obj.default, list): |
| 511 | + # Get the related model |
| 512 | + content_type = field_obj.related_object_type |
| 513 | + if content_type.app_label == APP_LABEL: |
| 514 | + # Custom object type |
| 515 | + from netbox_custom_objects.models import CustomObjectType |
| 516 | + custom_object_type_id = content_type.model.replace("table", "").replace("model", "") |
| 517 | + custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id) |
| 518 | + model = custom_object_type.get_model(skip_object_fields=True) |
| 519 | + else: |
| 520 | + # Regular NetBox model |
| 521 | + model = content_type.model_class() |
| 522 | + |
| 523 | + try: |
| 524 | + # Query the database to get the actual objects |
| 525 | + initial_objects = model.objects.filter(pk__in=field_obj.default) |
| 526 | + # Convert to list of IDs for ModelMultipleChoiceField |
| 527 | + initial_ids = list(initial_objects.values_list('pk', flat=True)) |
| 528 | + |
| 529 | + # Set the initial value in the form's initial data |
| 530 | + if 'initial' not in kwargs: |
| 531 | + kwargs['initial'] = {} |
| 532 | + kwargs['initial'][field_name] = initial_ids |
| 533 | + except Exception: |
| 534 | + # If there's an error, don't set initial values |
| 535 | + pass |
| 536 | + |
| 537 | + # Now call the parent __init__ with the modified kwargs |
| 538 | + forms.NetBoxModelForm.__init__(self, *args, **kwargs) |
| 539 | + |
503 | 540 | # Create a custom save method to properly handle M2M fields |
504 | 541 | def custom_save(self, commit=True): |
505 | 542 | # First save the instance to get the primary key |
|
0 commit comments