[PR #8793] [MERGED] Fixes #8715: eliminates duplicates when used in many-to-many field constraints #13395

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

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/8793
Author: @seros1521
Created: 3/4/2022
Status: Merged
Merged: 3/7/2022
Merged by: @jeremystretch

Base: developHead: fix_8715


📝 Commits (1)

  • 90257e9 Fixes #8715: eliminates duplicates when used in many-to-many field constraints

📊 Changes

1 file changed (+6 additions, -0 deletions)

View changed files

📝 netbox/utilities/querysets.py (+6 -0)

📄 Description

Fixes #8715

Some netbox users create object access rights using tags. Inside netbox, all object level permissions are implemented by the RestrictedQuerySet class:
3436905744/netbox/utilities/querysets.py
When using permissions that use tags, a user may receive multiple permissions of the same type if multiple tags are assigned to the device. This causes the RestrictedQuerySet class to generate a query similar to this:

>>> dcim.models.Device.objects.filter(Q(tags__name='tag1')|Q(tags__name='tag2'))
<ConfigContextModelQuerySet [<Device: device1>, <Device: device1>]>

This query returns the same object twice if both tags are assigned to it. This is due to the use of the django-taggit library. The library's documentation describes this behavior as expected and suggests using an explicit distinct() call in queries to avoid duplicates.
However, the use of DISTINCT in queries has a global side effect - deduplication of responses, which may or may not be acceptable behavior (depending on further use). Since it is not known how RestrictedQuerySet will be used in the rest of the code, it was decided to dedupe using a subquery.


🔄 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/8793 **Author:** [@seros1521](https://github.com/seros1521) **Created:** 3/4/2022 **Status:** ✅ Merged **Merged:** 3/7/2022 **Merged by:** [@jeremystretch](https://github.com/jeremystretch) **Base:** `develop` ← **Head:** `fix_8715` --- ### 📝 Commits (1) - [`90257e9`](https://github.com/netbox-community/netbox/commit/90257e9dee40e0317bcbd8cab81d99e6ab57c140) Fixes #8715: eliminates duplicates when used in many-to-many field constraints ### 📊 Changes **1 file changed** (+6 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `netbox/utilities/querysets.py` (+6 -0) </details> ### 📄 Description Fixes #8715 Some netbox users create object access rights using tags. Inside netbox, all object level permissions are implemented by the RestrictedQuerySet class: https://github.com/netbox-community/netbox/blob/3436905744c93fec7ba59a8b7d72ef4102f82334/netbox/utilities/querysets.py When using permissions that use tags, a user may receive multiple permissions of the same type if multiple tags are assigned to the device. This causes the RestrictedQuerySet class to generate a query similar to this: ``` >>> dcim.models.Device.objects.filter(Q(tags__name='tag1')|Q(tags__name='tag2')) <ConfigContextModelQuerySet [<Device: device1>, <Device: device1>]> ``` This query returns the same object twice if both tags are assigned to it. This is due to the use of the django-taggit library. The library's documentation describes this behavior as expected and suggests using an explicit distinct() call in queries to avoid duplicates. However, the use of DISTINCT in queries has a global side effect - deduplication of responses, which may or may not be acceptable behavior (depending on further use). Since it is not known how RestrictedQuerySet will be used in the rest of the code, it was decided to dedupe using a subquery. --- <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:27:09 +01:00
adam closed this issue 2025-12-29 22:27:09 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#13395