Only preload selected options for APISelect widgets #3104

Closed
opened 2025-12-29 18:25:40 +01:00 by adam · 2 comments
Owner

Originally created by @hSaria on GitHub (Jan 1, 2020).

Proposed Changes

Change APISelect and APISelectMultiple to only preload the selected fields.

Justification

The current implementation for APISelect and APISelectMultiple preloads all of the objects of the model. Even though paging of size 50 is performed on the API calls, the page will load with all of the objects, even ignoring what APISelect might be filtering out.

For example, all of the roles (11 through 99) are loaded in the device creation page. The APISelect itself paginates correctly, but I presume the default behavior of Django's forms.Select is making it so all of the options are loaded (not 100% sure about the last part).

<select name="device_role" class="netbox-select2-api form-control select2-hidden-accessible" data-url="/api/dcim/device-roles/" required="" placeholder="Device role" id="id_device_role" tabindex="-1" aria-hidden="true">
  <option value="" selected="">---------</option>
  <option value="11">role-11</option>
  <option value="12">role-12</option>
  <option value="13">role-13</option>  
...
  <option value="97">role-97</option>
  <option value="98">role-98</option>
  <option value="99">role-99</option>
</select>

By preventing those options from being loaded, the pages that use the APISelect and/or APISelectMultiple would load faster due to smaller pages.

It should be noted that selected options must be loaded in order for them to be rendered on the form. It's only the options that aren't selected which can be omitted as they are created dynamically when selected.

Originally created by @hSaria on GitHub (Jan 1, 2020). ### Proposed Changes Change `APISelect` and `APISelectMultiple` to only preload the selected fields. ### Justification The current implementation for `APISelect` and `APISelectMultiple` preloads all of the objects of the model. Even though paging of size 50 is performed on the API calls, the page will load with all of the objects, even ignoring what `APISelect` might be filtering out. For example, all of the roles (11 through 99) are loaded in the device creation page. The APISelect itself paginates correctly, but I presume the default behavior of Django's `forms.Select` is making it so all of the options are loaded (not 100% sure about the last part). ```html <select name="device_role" class="netbox-select2-api form-control select2-hidden-accessible" data-url="/api/dcim/device-roles/" required="" placeholder="Device role" id="id_device_role" tabindex="-1" aria-hidden="true"> <option value="" selected="">---------</option> <option value="11">role-11</option> <option value="12">role-12</option> <option value="13">role-13</option> ... <option value="97">role-97</option> <option value="98">role-98</option> <option value="99">role-99</option> </select> ``` By preventing those options from being loaded, the pages that use the `APISelect` and/or `APISelectMultiple` would load faster due to smaller pages. It should be noted that **selected** options must be loaded in order for them to be rendered on the form. It's only the options that aren't selected which can be omitted as they are created dynamically when selected.
adam added the type: bugstatus: accepted labels 2025-12-29 18:25:40 +01:00
adam closed this issue 2025-12-29 18:25:40 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 2, 2020):

Thanks for capturing this. It's something I've been wanting to address for a while now.

@jeremystretch commented on GitHub (Jan 2, 2020): Thanks for capturing this. It's something I've been wanting to address for a while now.
Author
Owner

@hSaria commented on GitHub (Jan 2, 2020):

It's really @DanSheps that should be thanked. He pointed out the inefficiency when I was working on #3814

@hSaria commented on GitHub (Jan 2, 2020): It's really @DanSheps that should be thanked. He pointed out the inefficiency when I was working on #3814
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3104