[PR #1780] [MERGED] Fix for #1778. #12262

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

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/1780
Author: @explody
Created: 12/16/2017
Status: Merged
Merged: 12/20/2017
Merged by: @jeremystretch

Base: developHead: fix_1778


📝 Commits (1)

📊 Changes

1 file changed (+7 additions, -1 deletions)

View changed files

📝 netbox/utilities/views.py (+7 -1)

📄 Description

This will set initial values for visible bulk-add form fields from query args.

Fixes:

#1778

This is a simple fix to allow bulk-addition of addresses to receive initial values set in the single-address-addition form, which are already passed as query args in the URL.

The approach I took is just to loop through the form's visible Meta fields, and build a dict of initial form field values from query args that match field names. (initial vals described here):

https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#providing-initial-values

Query args that don't match field names are ignored since we loop on meta fields, not the query args, and Django quietly ignores invalid initial values.

There are alternate approaches to comparing the fields but they seemed like micro optimizations and slightly less easy to read:

        fields = getattr(self.model_form._meta, 'fields', [])
        for field in set(fields).intersection(request.GET.keys()):
            if request.GET[field]:
                initial[field] = request.GET[field]

🔄 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/1780 **Author:** [@explody](https://github.com/explody) **Created:** 12/16/2017 **Status:** ✅ Merged **Merged:** 12/20/2017 **Merged by:** [@jeremystretch](https://github.com/jeremystretch) **Base:** `develop` ← **Head:** `fix_1778` --- ### 📝 Commits (1) - [`1cebc12`](https://github.com/netbox-community/netbox/commit/1cebc1248b37cb120f78836760329eb624403f18) Fix for #1778. ### 📊 Changes **1 file changed** (+7 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `netbox/utilities/views.py` (+7 -1) </details> ### 📄 Description This will set initial values for visible bulk-add form fields from query args. <!-- Thank you for your interest in contributing to NetBox! Please note that our contribution policy requires that a feature request or bug report be opened for approval prior to filing a pull request. This helps avoid wasting time and effort on something that we might not be able to accept. Please indicate the relevant feature request or bug report below. --> ### Fixes: #1778 <!-- Please include a summary of the proposed changes below. --> This is a simple fix to allow bulk-addition of addresses to receive initial values set in the single-address-addition form, which are already passed as query args in the URL. The approach I took is just to loop through the form's visible Meta fields, and build a dict of initial form field values from query args that match field names. (initial vals described here): https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#providing-initial-values Query args that don't match field names are ignored since we loop on meta fields, not the query args, and Django quietly ignores invalid initial values. There are alternate approaches to comparing the fields but they seemed like micro optimizations and slightly less easy to read: ```python fields = getattr(self.model_form._meta, 'fields', []) for field in set(fields).intersection(request.GET.keys()): if request.GET[field]: initial[field] = request.GET[field] ``` --- <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:20:32 +01:00
adam closed this issue 2025-12-29 22:20:32 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#12262