mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-22 03:56:53 -06:00
Fix bulk nullification of custom fields
This commit is contained in:
parent
3621b1a0d0
commit
d38620bad2
@ -71,6 +71,10 @@ class NetBoxModelBulkEditForm(BootstrapMixin, CustomFieldsMixin, forms.Form):
|
|||||||
"""
|
"""
|
||||||
nullable_fields = ()
|
nullable_fields = ()
|
||||||
|
|
||||||
|
pk = forms.ModelMultipleChoiceField(
|
||||||
|
queryset=None,
|
||||||
|
widget=forms.MultipleHiddenInput
|
||||||
|
)
|
||||||
add_tags = DynamicModelMultipleChoiceField(
|
add_tags = DynamicModelMultipleChoiceField(
|
||||||
queryset=Tag.objects.all(),
|
queryset=Tag.objects.all(),
|
||||||
required=False
|
required=False
|
||||||
@ -80,6 +84,10 @@ class NetBoxModelBulkEditForm(BootstrapMixin, CustomFieldsMixin, forms.Form):
|
|||||||
required=False
|
required=False
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.fields['pk'].queryset = self.model.objects.all()
|
||||||
|
|
||||||
def _get_form_field(self, customfield):
|
def _get_form_field(self, customfield):
|
||||||
return customfield.to_form_field(set_initial=False, enforce_required=False)
|
return customfield.to_form_field(set_initial=False, enforce_required=False)
|
||||||
|
|
||||||
@ -89,18 +97,19 @@ class NetBoxModelBulkEditForm(BootstrapMixin, CustomFieldsMixin, forms.Form):
|
|||||||
"""
|
"""
|
||||||
nullable_custom_fields = []
|
nullable_custom_fields = []
|
||||||
for customfield in self._get_custom_fields(self._get_content_type()):
|
for customfield in self._get_custom_fields(self._get_content_type()):
|
||||||
|
field_name = f'cf_{customfield.name}'
|
||||||
|
self.fields[field_name] = self._get_form_field(customfield)
|
||||||
|
|
||||||
# Record non-required custom fields as nullable
|
# Record non-required custom fields as nullable
|
||||||
if not customfield.required:
|
if not customfield.required:
|
||||||
nullable_custom_fields.append(customfield.name)
|
nullable_custom_fields.append(field_name)
|
||||||
|
|
||||||
self.fields[customfield.name] = self._get_form_field(customfield)
|
|
||||||
|
|
||||||
# Annotate the field in the list of CustomField form fields
|
# Annotate the field in the list of CustomField form fields
|
||||||
self.custom_fields[customfield.name] = customfield
|
self.custom_fields[field_name] = customfield
|
||||||
|
|
||||||
# Annotate nullable custom fields (if any) on the form instance
|
# Annotate nullable custom fields (if any) on the form instance
|
||||||
if nullable_custom_fields:
|
if nullable_custom_fields:
|
||||||
self.custom_fields = (*self.custom_fields, *nullable_custom_fields)
|
self.nullable_fields = (*self.nullable_fields, *nullable_custom_fields)
|
||||||
|
|
||||||
|
|
||||||
class NetBoxModelFilterSetForm(BootstrapMixin, CustomFieldsMixin, forms.Form):
|
class NetBoxModelFilterSetForm(BootstrapMixin, CustomFieldsMixin, forms.Form):
|
||||||
|
@ -481,10 +481,12 @@ class BulkEditView(GetReturnURLMixin, BaseMultiObjectView):
|
|||||||
|
|
||||||
# Update custom fields
|
# Update custom fields
|
||||||
for name in custom_fields:
|
for name in custom_fields:
|
||||||
|
assert name.startswith('cf_')
|
||||||
|
cf_name = name[3:] # Strip cf_ prefix
|
||||||
if name in form.nullable_fields and name in nullified_fields:
|
if name in form.nullable_fields and name in nullified_fields:
|
||||||
obj.custom_field_data[name] = None
|
obj.custom_field_data[cf_name] = None
|
||||||
elif name in form.changed_data:
|
elif name in form.changed_data:
|
||||||
obj.custom_field_data[name] = form.fields[name].prepare_value(form.cleaned_data[name])
|
obj.custom_field_data[cf_name] = form.fields[name].prepare_value(form.cleaned_data[name])
|
||||||
|
|
||||||
obj.full_clean()
|
obj.full_clean()
|
||||||
obj.save()
|
obj.save()
|
||||||
|
@ -36,10 +36,10 @@ class TenantGroupBulkEditForm(NetBoxModelBulkEditForm):
|
|||||||
|
|
||||||
|
|
||||||
class TenantBulkEditForm(NetBoxModelBulkEditForm):
|
class TenantBulkEditForm(NetBoxModelBulkEditForm):
|
||||||
pk = forms.ModelMultipleChoiceField(
|
# pk = forms.ModelMultipleChoiceField(
|
||||||
queryset=Tenant.objects.all(),
|
# queryset=Tenant.objects.all(),
|
||||||
widget=forms.MultipleHiddenInput()
|
# widget=forms.MultipleHiddenInput()
|
||||||
)
|
# )
|
||||||
group = DynamicModelChoiceField(
|
group = DynamicModelChoiceField(
|
||||||
queryset=TenantGroup.objects.all(),
|
queryset=TenantGroup.objects.all(),
|
||||||
required=False
|
required=False
|
||||||
|
Loading…
Reference in New Issue
Block a user