Skip to content

Script with MultiObjectVar throws TypeError if no 'data' is given and a null_option is used #9291

@PieterL75

Description

@PieterL75

NetBox version

v3.2.1

Python version

3.9

Steps to Reproduce

Create a Script with MultiObjectVar field
Set null_option='Any'

    env_aggregate = MultiObjectVar(
        label = "Aggregate",
        model = Aggregate,
        null_option = 'All',
        required = False,

Run the script through the 'manage runscript' without providing any data.

Expected Behavior

Script runs fine

Observed Behavior

A TypeError is raised in DynamicModelMultipleChoiceField.clean()

class DynamicModelMultipleChoiceField(DynamicModelChoiceMixin, forms.ModelMultipleChoiceField):
"""
A multiple-choice version of `DynamicModelChoiceField`.
"""
filter = django_filters.ModelMultipleChoiceFilter
widget = widgets.APISelectMultiple
def clean(self, value):
"""
When null option is enabled and "None" is sent as part of a form to be submitted, it is sent as the
string 'null'. This will check for that condition and gracefully handle the conversion to a NoneType.
"""
if self.null_option is not None and settings.FILTERS_NULL_CHOICE_VALUE in value:
value = [v for v in value if v != settings.FILTERS_NULL_CHOICE_VALUE]
return [None, *value]
return super().clean(value)

The 'clean(self , value) gets the the 'value' parameter as 'None', if no data is passed to the script.
The 'in value' fails, as "argument of type 'NoneType' is not iterable"

Metadata

Metadata

Assignees

Labels

status: acceptedThis issue has been accepted for implementationtype: bugA confirmed report of unexpected behavior in the application

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions