Expose DynamicModelChoiceField to plugin developers #4464

Closed
opened 2025-12-29 18:36:15 +01:00 by adam · 7 comments
Owner

Originally created by @MajesticFalcon on GitHub (Jan 14, 2021).

Environment

  • Python version: 3.8
  • NetBox version: 2.10

Proposed Functionality

As of now, plugins don't seem to be able to import DynamicModelChoiceField to gain the searchable select box. I proposed exposing this functionality as it would greatly increase the user experience of the plugins.

Use Case

Plugins that have an address foreign key. The issue becomes either use a searchable select or jump the user between multiple pages to "link" an address.

Database Changes

None known.

External Dependencies

None known

Originally created by @MajesticFalcon on GitHub (Jan 14, 2021). <!-- NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED. This form is only for proposing specific new features or enhancements. If you have a general idea or question, please start a discussion instead: https://github.com/netbox-community/netbox/discussions NOTE: Due to an excessive backlog of feature requests, we are not currently accepting any proposals which significantly extend NetBox's feature scope. Please describe the environment in which you are running NetBox. Be sure that you are running an unmodified instance of the latest stable release before submitting a bug report. --> ### Environment * Python version: 3.8 * NetBox version: 2.10 <!-- Describe in detail the new functionality you are proposing. Include any specific changes to work flows, data models, or the user interface. --> ### Proposed Functionality As of now, plugins don't seem to be able to import DynamicModelChoiceField to gain the searchable select box. I proposed exposing this functionality as it would greatly increase the user experience of the plugins. <!-- Convey an example use case for your proposed feature. Write from the perspective of a NetBox user who would benefit from the proposed functionality and describe how. ---> ### Use Case Plugins that have an address foreign key. The issue becomes either use a searchable select or jump the user between multiple pages to "link" an address. <!-- Note any changes to the database schema necessary to support the new feature. For example, does the proposal require adding a new model or field? (Not all new features require database changes.) ---> ### Database Changes None known. <!-- List any new dependencies on external libraries or services that this new feature would introduce. For example, does the proposal require the installation of a new Python package? (Not all new features introduce new dependencies.) --> ### External Dependencies None known
adam added the type: feature label 2025-12-29 18:36:15 +01:00
adam closed this issue 2025-12-29 18:36:15 +01:00
Author
Owner

@lampwins commented on GitHub (Jan 19, 2021):

The DynamicModelChoiceField and its siblings underwent a total rewrite not long ago which helped to make the implementation both easier to work with and more stable. I support this being included in the plugins API because it promotes consistent user experiences which ultimately makes end-users happier with NetBox. It does raise a larger question though, of what the process actually looks like to formally expose existing core components to the plugins API?

@lampwins commented on GitHub (Jan 19, 2021): The DynamicModelChoiceField and its siblings underwent a total rewrite not long ago which helped to make the implementation both easier to work with and more stable. I support this being included in the plugins API because it promotes consistent user experiences which ultimately makes end-users happier with NetBox. It does raise a larger question though, of what the process actually looks like to formally expose existing core components to the plugins API?
Author
Owner

@jeremystretch commented on GitHub (Jan 19, 2021):

DynamicModelChoiceField is not mature or stable enough for use outside of NetBox core. However, you can certainly replicate its functionality within your plugin.

It does raise a larger question though, of what the process actually looks like to formally expose existing core components to the plugins API?

That's a question we can ponder once we've worked through the years-long backlog of pending core feature requests. Let's not let the plugins API distract from work on the core product.

@jeremystretch commented on GitHub (Jan 19, 2021): DynamicModelChoiceField is not mature or stable enough for use outside of NetBox core. However, you can certainly replicate its functionality within your plugin. > It does raise a larger question though, of what the process actually looks like to formally expose existing core components to the plugins API? That's a question we can ponder once we've worked through the years-long backlog of pending core feature requests. Let's not let the plugins API distract from work on the core product.
Author
Owner

@MajesticFalcon commented on GitHub (Jan 19, 2021):

Fair enough. I am still familiarizing myself with the plugin ecosystem. Would you agree that this functionality could be written as a plugin itself and then re-used by other plugins? Or is there some technical barrier preventing such latching between plugins?

@MajesticFalcon commented on GitHub (Jan 19, 2021): Fair enough. I am still familiarizing myself with the plugin ecosystem. Would you agree that this functionality could be written as a plugin itself and then re-used by other plugins? Or is there some technical barrier preventing such latching between plugins?
Author
Owner

@jeremystretch commented on GitHub (Feb 4, 2021):

Would you agree that this functionality could be written as a plugin itself and then re-used by other plugins?

It wouldn't even need to be a plugin; a Python package would be sufficient.

@jeremystretch commented on GitHub (Feb 4, 2021): > Would you agree that this functionality could be written as a plugin itself and then re-used by other plugins? It wouldn't even need to be a plugin; a Python package would be sufficient.
Author
Owner

@ledgley commented on GitHub (Mar 3, 2021):

@MajesticFalcon try this and see if it does what you need

software = DynamicModelChoiceField(
    queryset=Software.objects.all(),
    required=True,
    widget=APISelect(
        api_url='/api/plugins/software/software/'
    ),
    query_params={
        'manufacturer': '$manufacturer'
    }
)
@ledgley commented on GitHub (Mar 3, 2021): @MajesticFalcon try this and see if it does what you need ```python software = DynamicModelChoiceField( queryset=Software.objects.all(), required=True, widget=APISelect( api_url='/api/plugins/software/software/' ), query_params={ 'manufacturer': '$manufacturer' } ) ```
Author
Owner

@MajesticFalcon commented on GitHub (Mar 26, 2021):

@MajesticFalcon try this and see if it does what you need

software = DynamicModelChoiceField(
    queryset=Software.objects.all(),
    required=True,
    widget=APISelect(
        api_url='/api/plugins/software/software/'
    ),
    query_params={
        'manufacturer': '$manufacturer'
    }
)

The API endpoint works but the dynamicchoicefield stays blank. In debug, I can see the correct API endpoint being hit, but the box stays empty

@MajesticFalcon commented on GitHub (Mar 26, 2021): > @MajesticFalcon try this and see if it does what you need > > ```python > software = DynamicModelChoiceField( > queryset=Software.objects.all(), > required=True, > widget=APISelect( > api_url='/api/plugins/software/software/' > ), > query_params={ > 'manufacturer': '$manufacturer' > } > ) > ``` The API endpoint works but the dynamicchoicefield stays blank. In debug, I can see the correct API endpoint being hit, but the box stays empty
Author
Owner

@MajesticFalcon commented on GitHub (Mar 26, 2021):

Looks like all I needed to do was add "display_field." Thank you so much!

@MajesticFalcon commented on GitHub (Mar 26, 2021): Looks like all I needed to do was add "display_field." Thank you so much!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4464