Fixes #4525: Allow passing initial data to custom script MultiObjectVar

This commit is contained in:
Jeremy Stretch 2020-05-26 15:34:29 -04:00
parent e54d441433
commit ccc31b2c7c
3 changed files with 16 additions and 6 deletions

View File

@ -15,6 +15,7 @@
### Bug Fixes ### Bug Fixes
* [#3304](https://github.com/netbox-community/netbox/issues/3304) - Fix caching invalidation issue related to device/virtual machine primary IP addresses * [#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 * [#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 * [#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 * [#4647](https://github.com/netbox-community/netbox/issues/4647) - Fix caching invalidation issue related to assigning new IP addresses to interfaces

View File

@ -432,11 +432,11 @@ class ScriptForm(BootstrapMixin, forms.Form):
def __init__(self, vars, *args, commit_default=True, **kwargs): def __init__(self, vars, *args, commit_default=True, **kwargs):
super().__init__(*args, **kwargs)
# Dynamically populate fields for variables # Dynamically populate fields for variables
for name, var in vars.items(): 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 # Toggle default commit behavior based on Meta option
if not commit_default: if not commit_default:

View File

@ -606,15 +606,18 @@ class DynamicModelChoiceMixin:
filter = django_filters.ModelChoiceFilter filter = django_filters.ModelChoiceFilter
widget = APISelect widget = APISelect
def __init__(self, *args, **kwargs): def _get_initial_value(self, initial_data, field_name):
super().__init__(*args, **kwargs) return initial_data.get(field_name)
def get_bound_field(self, form, field_name): def get_bound_field(self, form, field_name):
bound_field = BoundField(form, self, 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 # 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. # 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: if data:
filter = self.filter(field_name=self.to_field_name or 'pk', queryset=self.queryset) filter = self.filter(field_name=self.to_field_name or 'pk', queryset=self.queryset)
self.queryset = filter.filter(self.queryset, data) self.queryset = filter.filter(self.queryset, data)
@ -647,6 +650,12 @@ class DynamicModelMultipleChoiceField(DynamicModelChoiceMixin, forms.ModelMultip
filter = django_filters.ModelMultipleChoiceFilter filter = django_filters.ModelMultipleChoiceFilter
widget = APISelectMultiple 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): class LaxURLField(forms.URLField):
""" """