Skip to content

Commit

Permalink
Fixes #4525: Allow passing initial data to custom script MultiObjectVar
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed May 26, 2020
1 parent e54d441 commit ccc31b2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/version-2.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
### Bug Fixes

* [#3304](https://github.com/netbox-community/netbox/issues/3304) - Fix caching invalidation issue related to device/virtual machine primary IP addresses
* [#4525](https://github.com/netbox-community/netbox/issues/4525) - Allow passing initial data to custom script MultiObjectVar
* [#4644](https://github.com/netbox-community/netbox/issues/4644) - Fix ordering of services table by parent
* [#4646](https://github.com/netbox-community/netbox/issues/4646) - Correct UI link for reports with custom name
* [#4647](https://github.com/netbox-community/netbox/issues/4647) - Fix caching invalidation issue related to assigning new IP addresses to interfaces
Expand Down
6 changes: 3 additions & 3 deletions netbox/extras/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,11 @@ class ScriptForm(BootstrapMixin, forms.Form):

def __init__(self, vars, *args, commit_default=True, **kwargs):

super().__init__(*args, **kwargs)

# Dynamically populate fields for variables
for name, var in vars.items():
self.fields[name] = var.as_field()
self.base_fields[name] = var.as_field()

super().__init__(*args, **kwargs)

# Toggle default commit behavior based on Meta option
if not commit_default:
Expand Down
15 changes: 12 additions & 3 deletions netbox/utilities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,15 +606,18 @@ class DynamicModelChoiceMixin:
filter = django_filters.ModelChoiceFilter
widget = APISelect

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def _get_initial_value(self, initial_data, field_name):
return initial_data.get(field_name)

def get_bound_field(self, form, field_name):
bound_field = BoundField(form, self, field_name)

# Override initial() to allow passing multiple values
bound_field.initial = self._get_initial_value(form.initial, field_name)

# Modify the QuerySet of the field before we return it. Limit choices to any data already bound: Options
# will be populated on-demand via the APISelect widget.
data = self.prepare_value(bound_field.data or bound_field.initial)
data = bound_field.value()
if data:
filter = self.filter(field_name=self.to_field_name or 'pk', queryset=self.queryset)
self.queryset = filter.filter(self.queryset, data)
Expand Down Expand Up @@ -647,6 +650,12 @@ class DynamicModelMultipleChoiceField(DynamicModelChoiceMixin, forms.ModelMultip
filter = django_filters.ModelMultipleChoiceFilter
widget = APISelectMultiple

def _get_initial_value(self, initial_data, field_name):
# If a QueryDict has been passed as initial form data, get *all* listed values
if hasattr(initial_data, 'getlist'):
return initial_data.getlist(field_name)
return initial_data.get(field_name)


class LaxURLField(forms.URLField):
"""
Expand Down

0 comments on commit ccc31b2

Please sign in to comment.