Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk edit of models with M2M fields can clear the M2M field #12983

Closed
Urth opened this issue Jun 23, 2023 · 5 comments · Fixed by #13004
Closed

Bulk edit of models with M2M fields can clear the M2M field #12983

Urth opened this issue Jun 23, 2023 · 5 comments · Fixed by #13004
Assignees
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@Urth
Copy link
Contributor

Urth commented Jun 23, 2023

NetBox version

v3.5.4 and tested on develop

Python version

3.10

Steps to Reproduce

  1. Create an interface with one or more tagged_vlans.
  2. Use bulk edit on the interface to change something unrelated like the speed.
  3. Observe the changelog with the tagged_vlans being removed.

Expected Behavior

The untagged_vlans should remain unchanged if none are selected and set null is unchecked.

Observed Behavior

The untagged_vlans are removed from the interface.

Caused by https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/views/generic/bulk_views.py#L554-L555
In 41c9248 the truthy check on the m2m queryset was removed. Django querysets evaluate to False if the queryset is empty.

@Urth Urth added the type: bug A confirmed report of unexpected behavior in the application label Jun 23, 2023
@abhi1693 abhi1693 added status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation severity: medium Results in substantial degraded or broken functionality for specfic workflows labels Jun 23, 2023
@netopsab
Copy link
Contributor

netopsab commented Jun 24, 2023

Hello,

I'm facing this issue too... Also and for clarification, you mention the untagged_vlan field (excepted / observed behavior section). However, as you mentioned, this issue occurs with m2m fields, so with tagged_vlans (not untagged_vlan).

My proposal to fix this issue :

--- a/netbox/netbox/views/generic/bulk_views.py
+++ b/netbox/netbox/views/generic/bulk_views.py
@@ -551,7 +551,7 @@ class BulkEditView(GetReturnURLMixin, BaseMultiObjectView):
             for name, m2m_field in m2m_fields.items():
                 if name in form.nullable_fields and name in nullified_fields:
                     getattr(obj, name).clear()
-                else:
+                elif name in form.changed_data:
                     getattr(obj, name).set(form.cleaned_data[name])

@abhi1693
Copy link
Member

@netopsab Would you like to submit a pr?

@netopsab
Copy link
Contributor

why not ... :)

@abhi1693 abhi1693 added status: accepted This issue has been accepted for implementation and removed status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation labels Jun 24, 2023
netopsab added a commit to netopsab/netbox that referenced this issue Jun 24, 2023
Urth added a commit to Urth/netbox that referenced this issue Jun 26, 2023
…lected

Partially revert 41c9248 to restore
bulk edit with m2m fields. The m2m cleaned_data yields a empty queryset
when nothing is selected. By setting the m2m relation unless set null is
checked even when nothing is selected the m2m relation is always
cleared.

This commit only sets the m2m relation when a selection is made.
@Urth
Copy link
Contributor Author

Urth commented Jun 26, 2023

The change proposed by @netopsab does not fix the problem. The bulk edit form always includes m2m fields in the cleaned_data. Otherwise the old code would trigger a KeyError. The problem is that the cleaned_data is an empty queryset when nothing is selected. My PR checks if the queryset is non-empty like the code before 41c9248 was applied.

@DanSheps
Copy link
Member

The change proposed by @netopsab does not fix the problem. The bulk edit form always includes m2m fields in the cleaned_data. Otherwise the old code would trigger a KeyError. The problem is that the cleaned_data is an empty queryset when nothing is selected. My PR checks if the queryset is non-empty like the code before 41c9248 was applied.

@netopsab is checking against changed_data not cleaned_data.

His method is consistent with our rchecking in other areas. Both approaches are equally valid.

jeremystretch pushed a commit that referenced this issue Jun 27, 2023
Partially revert 41c9248 to restore
bulk edit with m2m fields. The m2m cleaned_data yields a empty queryset
when nothing is selected. By setting the m2m relation unless set null is
checked even when nothing is selected the m2m relation is always
cleared.

This commit only sets the m2m relation when a selection is made.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
4 participants