[PR #4987] [MERGED] Closes #4982: Overhaul the APISelect widget and support ObjectVar filtering #12952

Closed
opened 2025-12-29 22:24:33 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/4987
Author: @jeremystretch
Created: 8/12/2020
Status: Merged
Merged: 8/13/2020
Merged by: @jeremystretch

Base: develop-2.9Head: 4982-apiselect-improvements


📝 Commits (10+)

  • 2c64d45 Drop support for conditional_query_params on APISelect
  • fdc43f8 Add display_field kwarg to DynamicModelChoiceMixin
  • a831e52 Remove value_field from APISelect; reference to_field_name on field
  • f3fb859 Add query_params attribute to DynamicModelChoiceMixin
  • 8a8b4e7 Move null_option to DynamicModelChoiceMixin
  • b917403 Replace 'nullable' attribute with null_option
  • d8d752b Fix form for adding devices to a cluster
  • d752c36 Fix virtual chassis member add form
  • 100c8fe Add support for referencing peer field values in query_params
  • c45a57f Update cable termination forms to use DynamicModelChoiceField

📊 Changes

13 files changed (+713 additions, -1110 deletions)

View changed files

📝 docs/additional-features/custom-scripts.md (+48 -10)
📝 netbox/circuits/forms.py (+13 -31)
📝 netbox/dcim/forms.py (+357 -538)
📝 netbox/extras/forms.py (+9 -33)
📝 netbox/extras/scripts.py (+24 -10)
📝 netbox/ipam/forms.py (+76 -141)
📝 netbox/project-static/js/forms.js (+18 -42)
📝 netbox/secrets/forms.py (+5 -7)
📝 netbox/templates/virtualization/cluster_add_devices.html (+0 -30)
📝 netbox/tenancy/forms.py (+13 -26)
📝 netbox/utilities/forms/fields.py (+50 -0)
📝 netbox/utilities/forms/widgets.py (+9 -88)
📝 netbox/virtualization/forms.py (+91 -154)

📄 Description

Closes #4982:

Although the goal of #4982 was to enhance ObjectVar specifically, the bulk of the work in this PR comprises overhauling the APISelect widget and DynamicModelChoiceMixin to support those enhancements. Changes are summarized below.

  • Move display_field, disabled_indicator to DynamicModelChoiceMixin
  • Move null_option to DynamicModelChoiceMixin; change from boolean to string for custom labeling
  • Remove value_field from APISelect; reference to_field_name instead
  • Replace additional_query_params with query_params on DynamicModelChoiceMixin
  • Extend query_params to support referencing peer fields; remove filter_for on APISelect
  • Remove conditional_query_params
  • Update ObjectVar to accept model instead of a queryset (backward compatibility for the queryset argument is maintained)
  • Add display_field, query_params, and null_option kwargs to ObjectVar

For illustration, the following code was taken from the current implementation of DeviceForm:

    manufacturer = DynamicModelChoiceField(
        queryset=Manufacturer.objects.all(),
        required=False,
        widget=APISelect(
            filter_for={
                'device_type': 'manufacturer_id'
            }
        )
    )
    device_type = DynamicModelChoiceField(
        queryset=DeviceType.objects.all(),
        widget=APISelect(
            display_field='model'
        )
    )

The rewritten version is simplified to:

    manufacturer = DynamicModelChoiceField(
        queryset=Manufacturer.objects.all(),
        required=False
    )
    device_type = DynamicModelChoiceField(
        queryset=DeviceType.objects.all(),
        display_field='model',
        query_params={
            'manufacturer_id': '$manufacturer'
        }
    )

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/4987 **Author:** [@jeremystretch](https://github.com/jeremystretch) **Created:** 8/12/2020 **Status:** ✅ Merged **Merged:** 8/13/2020 **Merged by:** [@jeremystretch](https://github.com/jeremystretch) **Base:** `develop-2.9` ← **Head:** `4982-apiselect-improvements` --- ### 📝 Commits (10+) - [`2c64d45`](https://github.com/netbox-community/netbox/commit/2c64d45c697455760b9b9c136e7a8b1791ecc140) Drop support for conditional_query_params on APISelect - [`fdc43f8`](https://github.com/netbox-community/netbox/commit/fdc43f8279c720ce6118402dea66ae9e7b8b9740) Add display_field kwarg to DynamicModelChoiceMixin - [`a831e52`](https://github.com/netbox-community/netbox/commit/a831e525da52922f26ab311d8ac10c67a8a56b55) Remove value_field from APISelect; reference to_field_name on field - [`f3fb859`](https://github.com/netbox-community/netbox/commit/f3fb85933f40fa65f3fafdb5d64310482e5d76ab) Add query_params attribute to DynamicModelChoiceMixin - [`8a8b4e7`](https://github.com/netbox-community/netbox/commit/8a8b4e728a2cf1b074b600590d15100e2fa6c6cf) Move null_option to DynamicModelChoiceMixin - [`b917403`](https://github.com/netbox-community/netbox/commit/b917403c934060635141b8e770f166946ace4e8f) Replace 'nullable' attribute with null_option - [`d8d752b`](https://github.com/netbox-community/netbox/commit/d8d752b62313ffafef1f4e69f6c4c2d7fc1f071f) Fix form for adding devices to a cluster - [`d752c36`](https://github.com/netbox-community/netbox/commit/d752c36ea8cf07f0aa0fbe83280b30269715b2c7) Fix virtual chassis member add form - [`100c8fe`](https://github.com/netbox-community/netbox/commit/100c8fef206642b1b7a60176a80249fad72430e8) Add support for referencing peer field values in query_params - [`c45a57f`](https://github.com/netbox-community/netbox/commit/c45a57ff4399989dcd8d2153f06210736275f6e7) Update cable termination forms to use DynamicModelChoiceField ### 📊 Changes **13 files changed** (+713 additions, -1110 deletions) <details> <summary>View changed files</summary> 📝 `docs/additional-features/custom-scripts.md` (+48 -10) 📝 `netbox/circuits/forms.py` (+13 -31) 📝 `netbox/dcim/forms.py` (+357 -538) 📝 `netbox/extras/forms.py` (+9 -33) 📝 `netbox/extras/scripts.py` (+24 -10) 📝 `netbox/ipam/forms.py` (+76 -141) 📝 `netbox/project-static/js/forms.js` (+18 -42) 📝 `netbox/secrets/forms.py` (+5 -7) 📝 `netbox/templates/virtualization/cluster_add_devices.html` (+0 -30) 📝 `netbox/tenancy/forms.py` (+13 -26) 📝 `netbox/utilities/forms/fields.py` (+50 -0) 📝 `netbox/utilities/forms/widgets.py` (+9 -88) 📝 `netbox/virtualization/forms.py` (+91 -154) </details> ### 📄 Description ### Closes #4982: Although the goal of #4982 was to enhance ObjectVar specifically, the bulk of the work in this PR comprises overhauling the APISelect widget and DynamicModelChoiceMixin to support those enhancements. Changes are summarized below. - Move `display_field`, `disabled_indicator` to DynamicModelChoiceMixin - Move `null_option` to DynamicModelChoiceMixin; change from boolean to string for custom labeling - Remove `value_field` from APISelect; reference `to_field_name` instead - Replace `additional_query_params` with `query_params` on DynamicModelChoiceMixin - Extend `query_params` to support referencing peer fields; remove `filter_for` on APISelect - Remove `conditional_query_params` - Update ObjectVar to accept `model` instead of a queryset (backward compatibility for the `queryset` argument is maintained) - Add `display_field`, `query_params`, and `null_option` kwargs to ObjectVar For illustration, the following code was taken from the current implementation of `DeviceForm`: ```python manufacturer = DynamicModelChoiceField( queryset=Manufacturer.objects.all(), required=False, widget=APISelect( filter_for={ 'device_type': 'manufacturer_id' } ) ) device_type = DynamicModelChoiceField( queryset=DeviceType.objects.all(), widget=APISelect( display_field='model' ) ) ``` The rewritten version is simplified to: ```python manufacturer = DynamicModelChoiceField( queryset=Manufacturer.objects.all(), required=False ) device_type = DynamicModelChoiceField( queryset=DeviceType.objects.all(), display_field='model', query_params={ 'manufacturer_id': '$manufacturer' } ) ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-29 22:24:33 +01:00
adam closed this issue 2025-12-29 22:24:34 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#12952