@@ -56,9 +56,6 @@ def get_annotated_form_field(self, field, enforce_visibility=True, **kwargs):
56
56
57
57
return form_field
58
58
59
- # def get_bulk_edit_form_field(self, field, **kwargs):
60
- # raise NotImplementedError
61
-
62
59
def get_table_column_field (self , field , ** kwargs ):
63
60
raise NotImplementedError
64
61
@@ -90,26 +87,6 @@ def get_form_field(self, field, **kwargs):
90
87
required = field .required , initial = field .default , validators = validators
91
88
)
92
89
93
- # def get_serializer_field(self, field, **kwargs):
94
- # required = kwargs.get("required", False)
95
- # validators = kwargs.pop("validators", None) or []
96
- # # validators.append(self.validator)
97
- # return serializers.CharField(
98
- # **{
99
- # "required": required,
100
- # "allow_null": not required,
101
- # "allow_blank": not required,
102
- # "validators": validators,
103
- # **kwargs,
104
- # }
105
- # )
106
-
107
- # def get_bulk_edit_form_field(self, field, **kwargs):
108
- # return forms.CharField(
109
- # max_length=200,
110
- # required=False,
111
- # )
112
-
113
90
def get_filterform_field (self , field , ** kwargs ):
114
91
return forms .CharField (
115
92
label = field ,
@@ -144,13 +121,6 @@ def get_form_field(self, field, **kwargs):
144
121
validators = validators ,
145
122
)
146
123
147
- # def get_bulk_edit_form_field(self, field, **kwargs):
148
- # return forms.CharField(
149
- # label=field,
150
- # widget=forms.Textarea(),
151
- # required=False,
152
- # )
153
-
154
124
def render_table_column (self , value ):
155
125
return render_markdown (value )
156
126
@@ -227,12 +197,6 @@ def get_form_field(self, field, **kwargs):
227
197
required = field .required , initial = field .default , widget = DatePicker ()
228
198
)
229
199
230
- # def get_bulk_edit_form_field(self, field, **kwargs):
231
- # return forms.DateField(
232
- # required=False,
233
- # widget=DatePicker()
234
- # )
235
-
236
200
237
201
class DateTimeFieldType (FieldType ):
238
202
def get_model_field (self , field , ** kwargs ):
@@ -244,12 +208,6 @@ def get_form_field(self, field, **kwargs):
244
208
required = field .required , initial = field .default , widget = DateTimePicker ()
245
209
)
246
210
247
- # def get_bulk_edit_form_field(self, field, **kwargs):
248
- # return forms.DateTimeField(
249
- # required=False,
250
- # widget=DateTimePicker()
251
- # )
252
-
253
211
254
212
class URLFieldType (FieldType ):
255
213
def get_model_field (self , field , ** kwargs ):
@@ -366,12 +324,6 @@ def get_form_field(self, field, for_csv_import=False, **kwargs):
366
324
def render_table_column (self , value ):
367
325
return ", " .join (value )
368
326
369
- # TODO: Implement this
370
- # def get_bulk_edit_form_field(self, field, **kwargs):
371
- # return forms.MultipleChoiceField(
372
- # choices=field.choices, required=required, label=label, **kwargs
373
- # )
374
-
375
327
376
328
class ObjectFieldType (FieldType ):
377
329
def get_model_field (self , field , ** kwargs ):
@@ -397,28 +349,42 @@ def get_model_field(self, field, **kwargs):
397
349
)
398
350
return f
399
351
400
- # TODO: This logic is migrated from to_form_field, but currently does not work with custom objects as
401
- # the related_object_type (selected value is not recognized as an isinstance of the related model object)
402
- # def get_form_field(self, field, for_csv_import=False, **kwargs):
403
- # # return field.to_form_field()
404
- # model = field.related_object_type.model_class()
405
- # if not model:
406
- # CustomObjectType = apps.get_model('netbox_custom_objects.CustomObjectType')
407
- # custom_object_model_name = field.related_object_type.name
408
- # custom_object_type_id = custom_object_model_name.replace('table', '').replace('model', '')
409
- # custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id)
410
- # model = custom_object_type.get_model()
411
- # field_class = CSVModelChoiceField if for_csv_import else CustomObjectDynamicModelChoiceField
412
- # kwargs = {
413
- # 'queryset': model.objects.all(),
414
- # 'required': field.required,
415
- # 'initial': field.default,
416
- # }
417
- # if not for_csv_import:
418
- # kwargs['query_params'] = field.related_object_filter
419
- # kwargs['selector'] = True
420
- #
421
- # return field_class(**kwargs)
352
+ def get_form_field (self , field , for_csv_import = False , ** kwargs ):
353
+ """
354
+ Returns a form field for object relationships.
355
+ For custom objects, uses CustomObjectDynamicModelChoiceField.
356
+ For regular NetBox objects, uses DynamicModelChoiceField.
357
+ """
358
+ content_type = ContentType .objects .get (pk = field .related_object_type_id )
359
+
360
+ from utilities .forms .fields import DynamicModelChoiceField
361
+ if content_type .app_label == APP_LABEL :
362
+ # This is a custom object type
363
+ from netbox_custom_objects .models import CustomObjectType
364
+
365
+ custom_object_type_id = content_type .model .replace ("table" , "" ).replace (
366
+ "model" , ""
367
+ )
368
+ custom_object_type = CustomObjectType .objects .get (pk = custom_object_type_id )
369
+ model = custom_object_type .get_model ()
370
+ field_class = DynamicModelChoiceField
371
+ else :
372
+ # This is a regular NetBox model
373
+ model = content_type .model_class ()
374
+
375
+ field_class = DynamicModelChoiceField
376
+
377
+ return field_class (
378
+ queryset = model .objects .all (),
379
+ required = field .required ,
380
+ initial = field .default ,
381
+ query_params = (
382
+ field .related_object_filter
383
+ if hasattr (field , "related_object_filter" )
384
+ else None
385
+ ),
386
+ selector = True ,
387
+ )
422
388
423
389
def get_filterform_field (self , field , ** kwargs ):
424
390
return None
@@ -672,6 +638,40 @@ def get_model_field(self, field, **kwargs):
672
638
673
639
return m2m_field
674
640
641
+ def get_form_field (self , field , for_csv_import = False , ** kwargs ):
642
+ """
643
+ Returns a form field for multi-object relationships.
644
+ Uses DynamicModelMultipleChoiceField for both custom objects and regular NetBox objects.
645
+ """
646
+ content_type = ContentType .objects .get (pk = field .related_object_type_id )
647
+
648
+ if content_type .app_label == APP_LABEL :
649
+ # This is a custom object type
650
+ from netbox_custom_objects .models import CustomObjectType
651
+
652
+ custom_object_type_id = content_type .model .replace ("table" , "" ).replace (
653
+ "model" , ""
654
+ )
655
+ custom_object_type = CustomObjectType .objects .get (pk = custom_object_type_id )
656
+ model = custom_object_type .get_model ()
657
+ else :
658
+ # This is a regular NetBox model
659
+ model = content_type .model_class ()
660
+
661
+ from utilities .forms .fields import DynamicModelMultipleChoiceField
662
+
663
+ return DynamicModelMultipleChoiceField (
664
+ queryset = model .objects .all (),
665
+ required = field .required ,
666
+ initial = field .default ,
667
+ query_params = (
668
+ field .related_object_filter
669
+ if hasattr (field , "related_object_filter" )
670
+ else None
671
+ ),
672
+ selector = True ,
673
+ )
674
+
675
675
def get_filterform_field (self , field , ** kwargs ):
676
676
return None
677
677
0 commit comments