Don't use DynamicChoiceField for custom field choices #11208

Closed
opened 2025-12-29 21:41:57 +01:00 by adam · 1 comment
Owner

Originally created by @alehaa on GitHub (May 21, 2025).

NetBox version

v4.3.1

Feature type

Change to existing functionality

Proposed functionality

The current implementation of model forms uses an API call to retrieve available choices. I propose replacing this lookup with a static approach by replacing DynamicChoiceField with ChoiceField.

Use case

Using a DynamicChoiceField requires users to have privileges for the specific CustomFieldChoices instance, as the API endpoint needs to be reached. This complicates permission restrictions. Since the choices contain a static list, I don't believe this change will impact performance, but designing permissions will be easier.

Database changes

None.

External dependencies

None.

Originally created by @alehaa on GitHub (May 21, 2025). ### NetBox version v4.3.1 ### Feature type Change to existing functionality ### Proposed functionality The [current implementation](https://github.com/netbox-community/netbox/blob/main/netbox/extras/models/customfields.py#L518-L523) of model forms uses an API call to retrieve available choices. I propose replacing this lookup with a static approach by replacing `DynamicChoiceField` with `ChoiceField`. ### Use case Using a `DynamicChoiceField` requires users to have privileges for the specific `CustomFieldChoices` instance, as the API endpoint needs to be reached. This complicates permission restrictions. Since the choices contain a static list, I don't believe this change will impact performance, but designing permissions will be easier. ### Database changes None. ### External dependencies None.
adam added the type: feature label 2025-12-29 21:41:57 +01:00
adam closed this issue 2025-12-29 21:41:57 +01:00
Author
Owner

@jeremystretch commented on GitHub (May 22, 2025):

It's necessary to use DynamicChoiceField here because a choice set can potentially have many thousands of choices (UN/LOCODE, for example). Prepopulating these all in a static form field is suboptimal and can impose significant performance issues.

Using a DynamicChoiceField requires users to have privileges for the specific CustomFieldChoices instance, as the API endpoint needs to be reached. This complicates permission restrictions.

If this is a concern, you can add extras.CustomFieldChoiceSet to EXEMPT_VIEW_PERMISSIONS to obviate the need for manual permission assignment.

@jeremystretch commented on GitHub (May 22, 2025): It's necessary to use DynamicChoiceField here because a choice set can potentially have many thousands of choices (UN/LOCODE, for example). Prepopulating these all in a static form field is suboptimal and can impose significant performance issues. > Using a DynamicChoiceField requires users to have privileges for the specific CustomFieldChoices instance, as the API endpoint needs to be reached. This complicates permission restrictions. If this is a concern, you can add `extras.CustomFieldChoiceSet` to [`EXEMPT_VIEW_PERMISSIONS`](https://netboxlabs.com/docs/netbox/configuration/security/#exempt_view_permissions) to obviate the need for manual permission assignment.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11208