Skip to content

Commit bcffc38

Browse files
authored
Closes: #17936 - GFK serializer field (#20706)
* Establish GFKSerializerField and replace get_* methods in circuits.py * Set read_only=True * Apply GFKSerializerField to all matching SerializerMethodFields * Use GFKSerializerField for ObjectChangeSerializer.changed_object and EventRuleSerializer.action_object
1 parent 068d493 commit bcffc38

File tree

22 files changed

+119
-318
lines changed

22 files changed

+119
-318
lines changed

contrib/openapi.json

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -206935,8 +206935,8 @@
206935206935
"format": "int64"
206936206936
},
206937206937
"object": {
206938-
"nullable": true,
206939-
"readOnly": true
206938+
"readOnly": true,
206939+
"nullable": true
206940206940
},
206941206941
"user": {
206942206942
"$ref": "#/components/schemas/BriefUser"
@@ -211700,8 +211700,8 @@
211700211700
"readOnly": true
211701211701
},
211702211702
"termination": {
211703-
"nullable": true,
211704-
"readOnly": true
211703+
"readOnly": true,
211704+
"nullable": true
211705211705
},
211706211706
"created": {
211707211707
"type": "string",
@@ -211972,8 +211972,8 @@
211972211972
"nullable": true
211973211973
},
211974211974
"termination": {
211975-
"nullable": true,
211976-
"readOnly": true
211975+
"readOnly": true,
211976+
"nullable": true
211977211977
},
211978211978
"port_speed": {
211979211979
"type": "integer",
@@ -212176,8 +212176,8 @@
212176212176
"format": "int64"
212177212177
},
212178212178
"member": {
212179-
"nullable": true,
212180-
"readOnly": true
212179+
"readOnly": true,
212180+
"nullable": true
212181212181
},
212182212182
"priority": {
212183212183
"type": "object",
@@ -212561,8 +212561,8 @@
212561212561
"nullable": true
212562212562
},
212563212563
"termination": {
212564-
"nullable": true,
212565-
"readOnly": true
212564+
"readOnly": true,
212565+
"nullable": true
212566212566
},
212567212567
"port_speed": {
212568212568
"type": "integer",
@@ -212978,8 +212978,8 @@
212978212978
"nullable": true
212979212979
},
212980212980
"scope": {
212981-
"nullable": true,
212982-
"readOnly": true
212981+
"readOnly": true,
212982+
"nullable": true
212983212983
},
212984212984
"description": {
212985212985
"type": "string",
@@ -215293,9 +215293,8 @@
215293215293
"format": "int64"
215294215294
},
215295215295
"object": {
215296-
"type": "object",
215297-
"additionalProperties": {},
215298-
"readOnly": true
215296+
"readOnly": true,
215297+
"nullable": true
215299215298
},
215300215299
"contact": {
215301215300
"$ref": "#/components/schemas/BriefContact"
@@ -219029,9 +219028,8 @@
219029219028
"nullable": true
219030219029
},
219031219030
"action_object": {
219032-
"type": "object",
219033-
"additionalProperties": {},
219034-
"readOnly": true
219031+
"readOnly": true,
219032+
"nullable": true
219035219033
},
219036219034
"description": {
219037219035
"type": "string",
@@ -219536,8 +219534,8 @@
219536219534
"format": "int64"
219537219535
},
219538219536
"interface": {
219539-
"nullable": true,
219540-
"readOnly": true
219537+
"readOnly": true,
219538+
"nullable": true
219541219539
},
219542219540
"priority": {
219543219541
"type": "integer",
@@ -221339,8 +221337,8 @@
221339221337
"nullable": true
221340221338
},
221341221339
"assigned_object": {
221342-
"nullable": true,
221343-
"readOnly": true
221340+
"readOnly": true,
221341+
"nullable": true
221344221342
},
221345221343
"nat_inside": {
221346221344
"allOf": [
@@ -222501,8 +222499,8 @@
222501222499
"format": "int64"
222502222500
},
222503222501
"parent": {
222504-
"nullable": true,
222505-
"readOnly": true
222502+
"readOnly": true,
222503+
"nullable": true
222506222504
},
222507222505
"name": {
222508222506
"type": "string",
@@ -225660,8 +225658,8 @@
225660225658
"nullable": true
225661225659
},
225662225660
"component": {
225663-
"nullable": true,
225664-
"readOnly": true
225661+
"readOnly": true,
225662+
"nullable": true
225665225663
},
225666225664
"tags": {
225667225665
"type": "array",
@@ -226044,8 +226042,8 @@
226044226042
"nullable": true
226045226043
},
226046226044
"component": {
226047-
"nullable": true,
226048-
"readOnly": true
226045+
"readOnly": true,
226046+
"nullable": true
226049226047
},
226050226048
"created": {
226051226049
"type": "string",
@@ -226327,8 +226325,8 @@
226327226325
"format": "int64"
226328226326
},
226329226327
"assigned_object": {
226330-
"nullable": true,
226331-
"readOnly": true
226328+
"readOnly": true,
226329+
"nullable": true
226332226330
},
226333226331
"created": {
226334226332
"type": "string",
@@ -226777,8 +226775,8 @@
226777226775
"format": "int64"
226778226776
},
226779226777
"assigned_object": {
226780-
"nullable": true,
226781-
"readOnly": true
226778+
"readOnly": true,
226779+
"nullable": true
226782226780
},
226783226781
"tags": {
226784226782
"type": "array",
@@ -227155,8 +227153,8 @@
227155227153
"nullable": true
227156227154
},
227157227155
"assigned_object": {
227158-
"nullable": true,
227159-
"readOnly": true
227156+
"readOnly": true,
227157+
"nullable": true
227160227158
},
227161227159
"description": {
227162227160
"type": "string",
@@ -229542,8 +229540,8 @@
229542229540
"format": "int64"
229543229541
},
229544229542
"object": {
229545-
"nullable": true,
229546-
"readOnly": true
229543+
"readOnly": true,
229544+
"nullable": true
229547229545
},
229548229546
"user": {
229549229547
"$ref": "#/components/schemas/BriefUser"
@@ -229796,7 +229794,11 @@
229796229794
"format": "int64"
229797229795
},
229798229796
"changed_object": {
229799-
"nullable": true,
229797+
"readOnly": true,
229798+
"nullable": true
229799+
},
229800+
"object_repr": {
229801+
"type": "string",
229800229802
"readOnly": true
229801229803
},
229802229804
"message": {
@@ -229821,6 +229823,7 @@
229821229823
"display_url",
229822229824
"id",
229823229825
"message",
229826+
"object_repr",
229824229827
"postchange_data",
229825229828
"prechange_data",
229826229829
"request_id",
@@ -248892,8 +248895,8 @@
248892248895
"nullable": true
248893248896
},
248894248897
"scope": {
248895-
"nullable": true,
248896-
"readOnly": true
248898+
"readOnly": true,
248899+
"nullable": true
248897248900
},
248898248901
"tenant": {
248899248902
"allOf": [
@@ -252745,8 +252748,8 @@
252745252748
"format": "int64"
252746252749
},
252747252750
"parent": {
252748-
"nullable": true,
252749-
"readOnly": true
252751+
"readOnly": true,
252752+
"nullable": true
252750252753
},
252751252754
"name": {
252752252755
"type": "string",
@@ -253669,8 +253672,8 @@
253669253672
"format": "int64"
253670253673
},
253671253674
"object": {
253672-
"nullable": true,
253673-
"readOnly": true
253675+
"readOnly": true,
253676+
"nullable": true
253674253677
},
253675253678
"user": {
253676253679
"$ref": "#/components/schemas/BriefUser"
@@ -255193,8 +255196,8 @@
255193255196
"nullable": true
255194255197
},
255195255198
"termination": {
255196-
"nullable": true,
255197-
"readOnly": true
255199+
"readOnly": true,
255200+
"nullable": true
255198255201
},
255199255202
"outside_ip": {
255200255203
"allOf": [
@@ -255671,8 +255674,8 @@
255671255674
"nullable": true
255672255675
},
255673255676
"scope": {
255674-
"nullable": true,
255675-
"readOnly": true
255677+
"readOnly": true,
255678+
"nullable": true
255676255679
},
255677255680
"vid_ranges": {
255678255681
"type": "array",
@@ -258528,8 +258531,8 @@
258528258531
"nullable": true
258529258532
},
258530258533
"scope": {
258531-
"nullable": true,
258532-
"readOnly": true
258534+
"readOnly": true,
258535+
"nullable": true
258533258536
},
258534258537
"tenant": {
258535258538
"allOf": [

netbox/circuits/api/serializers_/circuits.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from django.contrib.contenttypes.models import ContentType
2-
from drf_spectacular.utils import extend_schema_field
32
from rest_framework import serializers
43

54
from circuits.choices import CircuitPriorityChoices, CircuitStatusChoices, VirtualCircuitTerminationRoleChoices
@@ -11,12 +10,12 @@
1110
from dcim.api.serializers_.device_components import InterfaceSerializer
1211
from dcim.api.serializers_.cables import CabledObjectSerializer
1312
from netbox.api.fields import ChoiceField, ContentTypeField, RelatedObjectCountField
13+
from netbox.api.gfk_fields import GFKSerializerField
1414
from netbox.api.serializers import (
1515
NetBoxModelSerializer, OrganizationalModelSerializer, PrimaryModelSerializer, WritableNestedSerializer,
1616
)
1717
from netbox.choices import DistanceUnitChoices
1818
from tenancy.api.serializers_.tenants import TenantSerializer
19-
from utilities.api import get_serializer_for_model
2019
from .providers import ProviderAccountSerializer, ProviderNetworkSerializer, ProviderSerializer
2120

2221
__all__ = (
@@ -55,7 +54,7 @@ class CircuitCircuitTerminationSerializer(WritableNestedSerializer):
5554
default=None
5655
)
5756
termination_id = serializers.IntegerField(allow_null=True, required=False, default=None)
58-
termination = serializers.SerializerMethodField(read_only=True)
57+
termination = GFKSerializerField(read_only=True)
5958

6059
class Meta:
6160
model = CircuitTermination
@@ -64,14 +63,6 @@ class Meta:
6463
'upstream_speed', 'xconnect_id', 'description',
6564
]
6665

67-
@extend_schema_field(serializers.JSONField(allow_null=True))
68-
def get_termination(self, obj):
69-
if obj.termination_id is None:
70-
return None
71-
serializer = get_serializer_for_model(obj.termination)
72-
context = {'request': self.context['request']}
73-
return serializer(obj.termination, nested=True, context=context).data
74-
7566

7667
class CircuitGroupSerializer(OrganizationalModelSerializer):
7768
tenant = TenantSerializer(nested=True, required=False, allow_null=True)
@@ -134,7 +125,7 @@ class CircuitTerminationSerializer(NetBoxModelSerializer, CabledObjectSerializer
134125
default=None
135126
)
136127
termination_id = serializers.IntegerField(allow_null=True, required=False, default=None)
137-
termination = serializers.SerializerMethodField(read_only=True)
128+
termination = GFKSerializerField(read_only=True)
138129

139130
class Meta:
140131
model = CircuitTermination
@@ -146,20 +137,12 @@ class Meta:
146137
]
147138
brief_fields = ('id', 'url', 'display', 'circuit', 'term_side', 'description', 'cable', '_occupied')
148139

149-
@extend_schema_field(serializers.JSONField(allow_null=True))
150-
def get_termination(self, obj):
151-
if obj.termination_id is None:
152-
return None
153-
serializer = get_serializer_for_model(obj.termination)
154-
context = {'request': self.context['request']}
155-
return serializer(obj.termination, nested=True, context=context).data
156-
157140

158141
class CircuitGroupAssignmentSerializer(CircuitGroupAssignmentSerializer_):
159142
member_type = ContentTypeField(
160143
queryset=ContentType.objects.filter(CIRCUIT_GROUP_ASSIGNMENT_MEMBER_MODELS)
161144
)
162-
member = serializers.SerializerMethodField(read_only=True)
145+
member = GFKSerializerField(read_only=True)
163146

164147
class Meta:
165148
model = CircuitGroupAssignment
@@ -169,14 +152,6 @@ class Meta:
169152
]
170153
brief_fields = ('id', 'url', 'display', 'group', 'member_type', 'member_id', 'member', 'priority')
171154

172-
@extend_schema_field(serializers.JSONField(allow_null=True))
173-
def get_member(self, obj):
174-
if obj.member_id is None:
175-
return None
176-
serializer = get_serializer_for_model(obj.member)
177-
context = {'request': self.context['request']}
178-
return serializer(obj.member, nested=True, context=context).data
179-
180155

181156
class VirtualCircuitTypeSerializer(OrganizationalModelSerializer):
182157

0 commit comments

Comments
 (0)