[PR #15122] [MERGED] Closes #15087: Support for specifying a subset of API serializer fields #14524

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

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/15122
Author: @jeremystretch
Created: 2/12/2024
Status: Merged
Merged: 2/14/2024
Merged by: @jeremystretch

Base: featureHead: 15087-dynamic-serializers


📝 Commits (8)

  • d999284 Enable dynamic field inclusion for REST API serializers
  • ab5090a Recurse through nested serializer when resolving prefetches
  • f251856 Remove obsolete calls to prefetch_related() for API views
  • 9157894 Remove support for brief_prefetch_fields viewset attribute
  • 0c3875d Rename query parameter
  • 1ebd0a0 Fixes #15133: Fix FHRP group representation on assignments endpoint under brief mode (#15134)
  • 96eaaac Restore get_queryset() on BriefModeMixin, minus prefetch logic
  • e4c93ed get_prefetches_for_serializer() should reference serializer field source if set

📊 Changes

16 files changed (+176 additions, -152 deletions)

View changed files

📝 netbox/circuits/api/views.py (+6 -11)
📝 netbox/core/api/views.py (+2 -2)
📝 netbox/dcim/api/views.py (+43 -68)
📝 netbox/extras/api/views.py (+4 -7)
📝 netbox/ipam/api/nested_serializers.py (+2 -1)
📝 netbox/ipam/api/views.py (+16 -25)
📝 netbox/ipam/tests/test_api.py (+1 -1)
📝 netbox/netbox/api/serializers/base.py (+9 -0)
📝 netbox/netbox/api/viewsets/__init__.py (+28 -0)
📝 netbox/netbox/api/viewsets/mixins.py (+0 -4)
📝 netbox/tenancy/api/views.py (+6 -8)
📝 netbox/users/api/views.py (+3 -3)
📝 netbox/utilities/api.py (+42 -0)
📝 netbox/virtualization/api/views.py (+8 -16)
📝 netbox/vpn/api/views.py (+4 -4)
📝 netbox/wireless/api/views.py (+2 -2)

📄 Description

Fixes: #15087

  • Introduces support for specifying a subset of serializer fields to be returned in a REST API request (e.g. GET /api/dcim/sites/?fields=id,name,status,region)
  • Introduces the get_prefetches_for_serializer() utility function
  • Extends the base API viewset to automatically resolve the necessary queryset prefetches for a serializer based on the requested fields
  • Removes all now-unnecessary explicit calls to prefetch_related() on API views (a few remain for various reasons)
  • Removes the brief_prefetch_fields property from API views

🔄 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/15122 **Author:** [@jeremystretch](https://github.com/jeremystretch) **Created:** 2/12/2024 **Status:** ✅ Merged **Merged:** 2/14/2024 **Merged by:** [@jeremystretch](https://github.com/jeremystretch) **Base:** `feature` ← **Head:** `15087-dynamic-serializers` --- ### 📝 Commits (8) - [`d999284`](https://github.com/netbox-community/netbox/commit/d99928435a3cf0e8f78b831d363426f4853410b1) Enable dynamic field inclusion for REST API serializers - [`ab5090a`](https://github.com/netbox-community/netbox/commit/ab5090af643677bd8e6229e032c92a797d6f1a98) Recurse through nested serializer when resolving prefetches - [`f251856`](https://github.com/netbox-community/netbox/commit/f25185661f16595b9931de1268cd99628b4a5dc3) Remove obsolete calls to prefetch_related() for API views - [`9157894`](https://github.com/netbox-community/netbox/commit/9157894cbc181e4a69ae16bfecc74deca4e82354) Remove support for brief_prefetch_fields viewset attribute - [`0c3875d`](https://github.com/netbox-community/netbox/commit/0c3875d0580c847ddf19efd21fb639f121be64c8) Rename query parameter - [`1ebd0a0`](https://github.com/netbox-community/netbox/commit/1ebd0a0e0f47eccf0fb6f861a6b19953f0672b16) Fixes #15133: Fix FHRP group representation on assignments endpoint under brief mode (#15134) - [`96eaaac`](https://github.com/netbox-community/netbox/commit/96eaaac2010c830f9df62c4cd46e4b519575d212) Restore get_queryset() on BriefModeMixin, minus prefetch logic - [`e4c93ed`](https://github.com/netbox-community/netbox/commit/e4c93edf247fdef2c0c5685b7941e21eb804de32) get_prefetches_for_serializer() should reference serializer field source if set ### 📊 Changes **16 files changed** (+176 additions, -152 deletions) <details> <summary>View changed files</summary> 📝 `netbox/circuits/api/views.py` (+6 -11) 📝 `netbox/core/api/views.py` (+2 -2) 📝 `netbox/dcim/api/views.py` (+43 -68) 📝 `netbox/extras/api/views.py` (+4 -7) 📝 `netbox/ipam/api/nested_serializers.py` (+2 -1) 📝 `netbox/ipam/api/views.py` (+16 -25) 📝 `netbox/ipam/tests/test_api.py` (+1 -1) 📝 `netbox/netbox/api/serializers/base.py` (+9 -0) 📝 `netbox/netbox/api/viewsets/__init__.py` (+28 -0) 📝 `netbox/netbox/api/viewsets/mixins.py` (+0 -4) 📝 `netbox/tenancy/api/views.py` (+6 -8) 📝 `netbox/users/api/views.py` (+3 -3) 📝 `netbox/utilities/api.py` (+42 -0) 📝 `netbox/virtualization/api/views.py` (+8 -16) 📝 `netbox/vpn/api/views.py` (+4 -4) 📝 `netbox/wireless/api/views.py` (+2 -2) </details> ### 📄 Description ### Fixes: #15087 - Introduces support for specifying a subset of serializer fields to be returned in a REST API request (e.g. `GET /api/dcim/sites/?fields=id,name,status,region`) - Introduces the `get_prefetches_for_serializer()` utility function - Extends the base API viewset to automatically resolve the necessary queryset prefetches for a serializer based on the requested fields - Removes all now-unnecessary explicit calls to `prefetch_related()` on API views (a few remain for various reasons) - Removes the `brief_prefetch_fields` property from API views --- <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 23:24:58 +01:00
adam closed this issue 2025-12-29 23:24:58 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#14524