[PR #15281] [MERGED] Closes #15278: Use primary serializers when representing nested objects #14568

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

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/15281
Author: @jeremystretch
Created: 2/26/2024
Status: Merged
Merged: 3/5/2024
Merged by: @jeremystretch

Base: featureHead: 15278-primary-nested-serializers


📝 Commits (7)

  • c146f5e Replace nested serializers with primary serializers where possible
  • c382ba0 Refactor REST API serializers to avoid circular imports
  • ca56c8b Add dynamic nesting support to SerializedPKRelatedField
  • 78e284c Initialize dynamically-resolved serializers with nested=True
  • 7008ffe Rename 'requested_fields' kwarg to 'fields' on BaseSerializer
  • 3bd28e2 Improve serializer initialization performance
  • cd74e04 Merge branch 'feature' into 15278-primary-nested-serializers

📊 Changes

84 files changed (+4213 additions, -3635 deletions)

View changed files

📝 netbox/circuits/api/serializers.py (+2 -144)
netbox/circuits/api/serializers_/__init__.py (+0 -0)
netbox/circuits/api/serializers_/circuits.py (+81 -0)
netbox/circuits/api/serializers_/providers.py (+68 -0)
📝 netbox/core/api/nested_serializers.py (+3 -2)
📝 netbox/core/api/serializers.py (+2 -73)
netbox/core/api/serializers_/__init__.py (+0 -0)
netbox/core/api/serializers_/data.py (+53 -0)
netbox/core/api/serializers_/jobs.py (+31 -0)
📝 netbox/dcim/api/nested_serializers.py (+0 -22)
📝 netbox/dcim/api/serializers.py (+13 -1305)
netbox/dcim/api/serializers_/__init__.py (+0 -0)
netbox/dcim/api/serializers_/base.py (+37 -0)
netbox/dcim/api/serializers_/cables.py (+126 -0)
netbox/dcim/api/serializers_/device_components.py (+368 -0)
netbox/dcim/api/serializers_/devices.py (+157 -0)
netbox/dcim/api/serializers_/devicetype_components.py (+327 -0)
netbox/dcim/api/serializers_/devicetypes.py (+74 -0)
netbox/dcim/api/serializers_/manufacturers.py (+26 -0)
netbox/dcim/api/serializers_/platforms.py (+29 -0)

...and 64 more files

📄 Description

Closes: #15278

  • Refactor the api.serializers module within each app, breaking out serializer classes by theme into submodules.
    • serializers.py is retained to preserve the public API, with submodules residing in api.serializers_ (note the trailing underscore).
  • Extend BaseModelSerializer for use in nested representations. This entails accepting a new nested=True keyword argument; when passed, the serializer returns only fields listed in brief_fields, and to_internal_value() is modified to accept a primary key or attributes referencing the related object.
    • The logic for referencing a related object by PK/attrs has been moved to get_related_object_by_attrs() for portability.
  • Extend SerializedPKRelatedField to accept the nested keyword argument and pass it to the serializer in use.
  • Update all calls to get_serializer_for_model() to omit the "Nested" prefix, instead passing nested=True on the primary serializer it returns.
  • Rename requested_fields keyword argument to fields on BaseSerializer

🔄 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/15281 **Author:** [@jeremystretch](https://github.com/jeremystretch) **Created:** 2/26/2024 **Status:** ✅ Merged **Merged:** 3/5/2024 **Merged by:** [@jeremystretch](https://github.com/jeremystretch) **Base:** `feature` ← **Head:** `15278-primary-nested-serializers` --- ### 📝 Commits (7) - [`c146f5e`](https://github.com/netbox-community/netbox/commit/c146f5e1b56f7c0e2eecd932d94ba21e43936392) Replace nested serializers with primary serializers where possible - [`c382ba0`](https://github.com/netbox-community/netbox/commit/c382ba0ae0d630a198fe350753481cb3e71d6ec8) Refactor REST API serializers to avoid circular imports - [`ca56c8b`](https://github.com/netbox-community/netbox/commit/ca56c8b9ef8a9c2238090b0a500e85361f9655b9) Add dynamic nesting support to SerializedPKRelatedField - [`78e284c`](https://github.com/netbox-community/netbox/commit/78e284c14f05eeb18d67ba90ed80a6b75e9d8cc6) Initialize dynamically-resolved serializers with nested=True - [`7008ffe`](https://github.com/netbox-community/netbox/commit/7008ffe6d8e730375b7b03198d20ee09db4c230f) Rename 'requested_fields' kwarg to 'fields' on BaseSerializer - [`3bd28e2`](https://github.com/netbox-community/netbox/commit/3bd28e2efe9d39d9992245c955d406c77b519e4b) Improve serializer initialization performance - [`cd74e04`](https://github.com/netbox-community/netbox/commit/cd74e040c115f6c7d9aa3b2c8f10b05f0540fc1a) Merge branch 'feature' into 15278-primary-nested-serializers ### 📊 Changes **84 files changed** (+4213 additions, -3635 deletions) <details> <summary>View changed files</summary> 📝 `netbox/circuits/api/serializers.py` (+2 -144) ➕ `netbox/circuits/api/serializers_/__init__.py` (+0 -0) ➕ `netbox/circuits/api/serializers_/circuits.py` (+81 -0) ➕ `netbox/circuits/api/serializers_/providers.py` (+68 -0) 📝 `netbox/core/api/nested_serializers.py` (+3 -2) 📝 `netbox/core/api/serializers.py` (+2 -73) ➕ `netbox/core/api/serializers_/__init__.py` (+0 -0) ➕ `netbox/core/api/serializers_/data.py` (+53 -0) ➕ `netbox/core/api/serializers_/jobs.py` (+31 -0) 📝 `netbox/dcim/api/nested_serializers.py` (+0 -22) 📝 `netbox/dcim/api/serializers.py` (+13 -1305) ➕ `netbox/dcim/api/serializers_/__init__.py` (+0 -0) ➕ `netbox/dcim/api/serializers_/base.py` (+37 -0) ➕ `netbox/dcim/api/serializers_/cables.py` (+126 -0) ➕ `netbox/dcim/api/serializers_/device_components.py` (+368 -0) ➕ `netbox/dcim/api/serializers_/devices.py` (+157 -0) ➕ `netbox/dcim/api/serializers_/devicetype_components.py` (+327 -0) ➕ `netbox/dcim/api/serializers_/devicetypes.py` (+74 -0) ➕ `netbox/dcim/api/serializers_/manufacturers.py` (+26 -0) ➕ `netbox/dcim/api/serializers_/platforms.py` (+29 -0) _...and 64 more files_ </details> ### 📄 Description ### Closes: #15278 - Refactor the `api.serializers` module within each app, breaking out serializer classes by theme into submodules. - `serializers.py` is retained to preserve the public API, with submodules residing in `api.serializers_` (note the trailing underscore). - Extend `BaseModelSerializer` for use in nested representations. This entails accepting a new `nested=True` keyword argument; when passed, the serializer returns only fields listed in `brief_fields`, and `to_internal_value()` is modified to accept a primary key or attributes referencing the related object. - The logic for referencing a related object by PK/attrs has been moved to `get_related_object_by_attrs()` for portability. - Extend `SerializedPKRelatedField` to accept the `nested` keyword argument and pass it to the serializer in use. - Update all calls to `get_serializer_for_model()` to omit the "Nested" prefix, instead passing `nested=True` on the primary serializer it returns. - Rename `requested_fields` keyword argument to `fields` on BaseSerializer --- <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:25:14 +01:00
adam closed this issue 2025-12-29 23:25:14 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#14568