Skip to content

Commit 1d54924

Browse files
authored
Merge pull request #120 from netboxlabs/105-bulk-edit
105 Fix bulk edit of object and mutliobject fields
2 parents c3bf8a1 + a49ee81 commit 1d54924

File tree

1 file changed

+70
-70
lines changed

1 file changed

+70
-70
lines changed

netbox_custom_objects/field_types.py

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ def get_annotated_form_field(self, field, enforce_visibility=True, **kwargs):
5656

5757
return form_field
5858

59-
# def get_bulk_edit_form_field(self, field, **kwargs):
60-
# raise NotImplementedError
61-
6259
def get_table_column_field(self, field, **kwargs):
6360
raise NotImplementedError
6461

@@ -90,26 +87,6 @@ def get_form_field(self, field, **kwargs):
9087
required=field.required, initial=field.default, validators=validators
9188
)
9289

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-
11390
def get_filterform_field(self, field, **kwargs):
11491
return forms.CharField(
11592
label=field,
@@ -144,13 +121,6 @@ def get_form_field(self, field, **kwargs):
144121
validators=validators,
145122
)
146123

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-
154124
def render_table_column(self, value):
155125
return render_markdown(value)
156126

@@ -227,12 +197,6 @@ def get_form_field(self, field, **kwargs):
227197
required=field.required, initial=field.default, widget=DatePicker()
228198
)
229199

230-
# def get_bulk_edit_form_field(self, field, **kwargs):
231-
# return forms.DateField(
232-
# required=False,
233-
# widget=DatePicker()
234-
# )
235-
236200

237201
class DateTimeFieldType(FieldType):
238202
def get_model_field(self, field, **kwargs):
@@ -244,12 +208,6 @@ def get_form_field(self, field, **kwargs):
244208
required=field.required, initial=field.default, widget=DateTimePicker()
245209
)
246210

247-
# def get_bulk_edit_form_field(self, field, **kwargs):
248-
# return forms.DateTimeField(
249-
# required=False,
250-
# widget=DateTimePicker()
251-
# )
252-
253211

254212
class URLFieldType(FieldType):
255213
def get_model_field(self, field, **kwargs):
@@ -366,12 +324,6 @@ def get_form_field(self, field, for_csv_import=False, **kwargs):
366324
def render_table_column(self, value):
367325
return ", ".join(value)
368326

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-
375327

376328
class ObjectFieldType(FieldType):
377329
def get_model_field(self, field, **kwargs):
@@ -397,28 +349,42 @@ def get_model_field(self, field, **kwargs):
397349
)
398350
return f
399351

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+
)
422388

423389
def get_filterform_field(self, field, **kwargs):
424390
return None
@@ -672,6 +638,40 @@ def get_model_field(self, field, **kwargs):
672638

673639
return m2m_field
674640

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+
675675
def get_filterform_field(self, field, **kwargs):
676676
return None
677677

0 commit comments

Comments
 (0)