Skip to content

Conversation

@kkthxbye-code
Copy link
Contributor

@kkthxbye-code kkthxbye-code commented May 27, 2022

Fixes: #9430

When pre-filling a MultiObjectVar for custom script execution passing a single pk above 9 would not work. Passing multiple values like: ?devices=1&device=99 works.

The error occurs in line 95 here:

# 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.value()
if data:
field_name = getattr(self, 'to_field_name') or 'pk'
filter = self.filter(field_name=field_name)
try:
self.queryset = filter.filter(self.queryset, data)
except (TypeError, ValueError):
# Catch any error caused by invalid initial data passed from the user
self.queryset = self.queryset.none()
else:
self.queryset = self.queryset.none()

When passed a two or more digit number in a string, the value seems to get split into a string, so instead of looking up pk 123, it looks up pk 1, 2 and 3.

I'm not sure exactly what's going on here so there might be a better place to fix the issue, please let me know and I'll change it.

@kkthxbye-code kkthxbye-code changed the title Make sure initial data is passed as array for DynamicModelChoiceFields Fixes #9435 - Make sure initial data is passed as array for DynamicModelChoiceFields May 27, 2022
@jsenecal
Copy link
Contributor

Good catch! I was struggling to find where the issue was occurring.

Could you add a test for that edge-case for good measure?

@jeremystretch jeremystretch merged commit 2c8a1ed into netbox-community:develop May 31, 2022
@jeremystretch jeremystretch changed the title Fixes #9435 - Make sure initial data is passed as array for DynamicModelChoiceFields Fixes #9430 - Make sure initial data is passed as array for DynamicModelChoiceFields May 31, 2022
@kkthxbye-code kkthxbye-code deleted the fix-9430 branch June 2, 2022 19:52
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow MultiObjectVar to accept PKs higher than 9 when script is called with a single URL param

3 participants