# Buggy code
def get_bound_field(self, form, field_name):
    bound_field = BoundField(form, self, 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.
    field_name = "{}{}".format(self.to_field_name or "pk", self.field_modifier)
    if bound_field.data:
        self.queryset = self.queryset.filter(
            **{field_name: self.prepare_value(bound_field.data)}
        )
    elif bound_field.initial:
        self.queryset = self.queryset.filter(
            **{field_name: self.prepare_value(bound_field.initial)}
        )
    else:
        self.queryset = self.queryset.none()

    return bound_field


# Traceback (ValueError)
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/contrib/auth/mixins.py", line 85, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/opt/netbox-dev/netbox/utilities/views.py", line 187, in get
return render(request, self.template_name, context)
File "/usr/local/lib/python3.6/site-packages/django/shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/usr/local/lib/python3.6/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/usr/local/lib/python3.6/site-packages/django/test/utils.py", line 96, in instrumented_test_render
return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/usr/local/lib/python3.6/site-packages/django/test/utils.py", line 96, in instrumented_test_render
return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/usr/local/lib/python3.6/site-packages/django/test/utils.py", line 96, in instrumented_test_render
return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py", line 309, in render
return nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 188, in render
return template.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 173, in render
return self._render(context)
File "/usr/local/lib/python3.6/site-packages/django/test/utils.py", line 96, in instrumented_test_render
return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py", line 165, in render
values = list(values)
File "/usr/local/lib/python3.6/site-packages/django/forms/forms.py", line 158, in __iter__
yield self[name]
File "/usr/local/lib/python3.6/site-packages/django/forms/forms.py", line 173, in __getitem__
self._bound_fields_cache[name] = field.get_bound_field(self, name)
File "/opt/netbox-dev/netbox/utilities/forms.py", line 576, in get_bound_field
self.queryset = self.queryset.filter(**{field_name: self.prepare_value(bound_field.data)})
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 892, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 910, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1290, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1318, in _add_q
split_subq=split_subq, simple_col=simple_col,
File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1251, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1116, in build_lookup
lookup = lookup_class(lhs, rhs)
File "/usr/local/lib/python3.6/site-packages/django/db/models/lookups.py", line 20, in __init__
self.rhs = self.get_prep_lookup()
File "/usr/local/lib/python3.6/site-packages/django/db/models/lookups.py", line 210, in get_prep_lookup
rhs_value = self.lhs.output_field.get_prep_value(rhs_value)
File "/usr/local/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 972, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'null'

# Fixed code
def get_bound_field(self, form, field_name):
    bound_field = BoundField(form, self, 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 = bound_field.data or bound_field.initial
    if data:
        filter = self.filter(
            field_name=self.to_field_name or "pk", queryset=self.queryset
        )
        self.queryset = filter.filter(self.queryset, data)
    else:
        self.queryset = self.queryset.none()

    return bound_field