Skip to content

Commit 7b5349e

Browse files
committed
Apply GFKSerializerField to all matching SerializerMethodFields
1 parent e08810a commit 7b5349e

File tree

20 files changed

+75
-240
lines changed

20 files changed

+75
-240
lines changed

contrib/openapi.json

Lines changed: 38 additions & 39 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",
@@ -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"
@@ -219536,8 +219535,8 @@
219536219535
"format": "int64"
219537219536
},
219538219537
"interface": {
219539-
"nullable": true,
219540-
"readOnly": true
219538+
"readOnly": true,
219539+
"nullable": true
219541219540
},
219542219541
"priority": {
219543219542
"type": "integer",
@@ -221339,8 +221338,8 @@
221339221338
"nullable": true
221340221339
},
221341221340
"assigned_object": {
221342-
"nullable": true,
221343-
"readOnly": true
221341+
"readOnly": true,
221342+
"nullable": true
221344221343
},
221345221344
"nat_inside": {
221346221345
"allOf": [
@@ -222501,8 +222500,8 @@
222501222500
"format": "int64"
222502222501
},
222503222502
"parent": {
222504-
"nullable": true,
222505-
"readOnly": true
222503+
"readOnly": true,
222504+
"nullable": true
222506222505
},
222507222506
"name": {
222508222507
"type": "string",
@@ -225660,8 +225659,8 @@
225660225659
"nullable": true
225661225660
},
225662225661
"component": {
225663-
"nullable": true,
225664-
"readOnly": true
225662+
"readOnly": true,
225663+
"nullable": true
225665225664
},
225666225665
"tags": {
225667225666
"type": "array",
@@ -226044,8 +226043,8 @@
226044226043
"nullable": true
226045226044
},
226046226045
"component": {
226047-
"nullable": true,
226048-
"readOnly": true
226046+
"readOnly": true,
226047+
"nullable": true
226049226048
},
226050226049
"created": {
226051226050
"type": "string",
@@ -226327,8 +226326,8 @@
226327226326
"format": "int64"
226328226327
},
226329226328
"assigned_object": {
226330-
"nullable": true,
226331-
"readOnly": true
226329+
"readOnly": true,
226330+
"nullable": true
226332226331
},
226333226332
"created": {
226334226333
"type": "string",
@@ -226777,8 +226776,8 @@
226777226776
"format": "int64"
226778226777
},
226779226778
"assigned_object": {
226780-
"nullable": true,
226781-
"readOnly": true
226779+
"readOnly": true,
226780+
"nullable": true
226782226781
},
226783226782
"tags": {
226784226783
"type": "array",
@@ -227155,8 +227154,8 @@
227155227154
"nullable": true
227156227155
},
227157227156
"assigned_object": {
227158-
"nullable": true,
227159-
"readOnly": true
227157+
"readOnly": true,
227158+
"nullable": true
227160227159
},
227161227160
"description": {
227162227161
"type": "string",
@@ -229542,8 +229541,8 @@
229542229541
"format": "int64"
229543229542
},
229544229543
"object": {
229545-
"nullable": true,
229546-
"readOnly": true
229544+
"readOnly": true,
229545+
"nullable": true
229547229546
},
229548229547
"user": {
229549229548
"$ref": "#/components/schemas/BriefUser"
@@ -248892,8 +248891,8 @@
248892248891
"nullable": true
248893248892
},
248894248893
"scope": {
248895-
"nullable": true,
248896-
"readOnly": true
248894+
"readOnly": true,
248895+
"nullable": true
248897248896
},
248898248897
"tenant": {
248899248898
"allOf": [
@@ -252745,8 +252744,8 @@
252745252744
"format": "int64"
252746252745
},
252747252746
"parent": {
252748-
"nullable": true,
252749-
"readOnly": true
252747+
"readOnly": true,
252748+
"nullable": true
252750252749
},
252751252750
"name": {
252752252751
"type": "string",
@@ -253669,8 +253668,8 @@
253669253668
"format": "int64"
253670253669
},
253671253670
"object": {
253672-
"nullable": true,
253673-
"readOnly": true
253671+
"readOnly": true,
253672+
"nullable": true
253674253673
},
253675253674
"user": {
253676253675
"$ref": "#/components/schemas/BriefUser"
@@ -255193,8 +255192,8 @@
255193255192
"nullable": true
255194255193
},
255195255194
"termination": {
255196-
"nullable": true,
255197-
"readOnly": true
255195+
"readOnly": true,
255196+
"nullable": true
255198255197
},
255199255198
"outside_ip": {
255200255199
"allOf": [
@@ -255671,8 +255670,8 @@
255671255670
"nullable": true
255672255671
},
255673255672
"scope": {
255674-
"nullable": true,
255675-
"readOnly": true
255673+
"readOnly": true,
255674+
"nullable": true
255676255675
},
255677255676
"vid_ranges": {
255678255677
"type": "array",
@@ -258528,8 +258527,8 @@
258528258527
"nullable": true
258529258528
},
258530258529
"scope": {
258531-
"nullable": true,
258532-
"readOnly": true
258530+
"readOnly": true,
258531+
"nullable": true
258533258532
},
258534258533
"tenant": {
258535258534
"allOf": [

netbox/circuits/api/serializers_/circuits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from dcim.api.serializers_.device_components import InterfaceSerializer
1111
from dcim.api.serializers_.cables import CabledObjectSerializer
1212
from netbox.api.fields import ChoiceField, ContentTypeField, RelatedObjectCountField
13-
from netbox.api.fields2 import GFKSerializerField
13+
from netbox.api.gfk_fields import GFKSerializerField
1414
from netbox.api.serializers import (
1515
NetBoxModelSerializer, OrganizationalModelSerializer, PrimaryModelSerializer, WritableNestedSerializer,
1616
)

netbox/dcim/api/serializers_/cables.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from dcim.choices import *
66
from dcim.models import Cable, CablePath, CableTermination
77
from netbox.api.fields import ChoiceField, ContentTypeField
8+
from netbox.api.gfk_fields import GFKSerializerField
89
from netbox.api.serializers import (
910
BaseModelSerializer, GenericObjectSerializer, NetBoxModelSerializer, PrimaryModelSerializer,
1011
)
@@ -53,9 +54,7 @@ class CableTerminationSerializer(NetBoxModelSerializer):
5354
termination_type = ContentTypeField(
5455
read_only=True,
5556
)
56-
termination = serializers.SerializerMethodField(
57-
read_only=True,
58-
)
57+
termination = GFKSerializerField(read_only=True)
5958

6059
class Meta:
6160
model = CableTermination
@@ -66,12 +65,6 @@ class Meta:
6665
read_only_fields = fields
6766
brief_fields = ('id', 'url', 'display', 'cable', 'cable_end', 'termination_type', 'termination_id')
6867

69-
@extend_schema_field(serializers.JSONField(allow_null=True))
70-
def get_termination(self, obj):
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-
7568

7669
class CablePathSerializer(serializers.ModelSerializer):
7770
path = serializers.SerializerMethodField(read_only=True)

netbox/dcim/api/serializers_/device_components.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from django.utils.translation import gettext as _
22
from django.contrib.contenttypes.models import ContentType
3-
from drf_spectacular.utils import extend_schema_field
43
from rest_framework import serializers
54

65
from dcim.choices import *
@@ -13,8 +12,8 @@
1312
from ipam.api.serializers_.vrfs import VRFSerializer
1413
from ipam.models import VLAN
1514
from netbox.api.fields import ChoiceField, ContentTypeField, SerializedPKRelatedField
15+
from netbox.api.gfk_fields import GFKSerializerField
1616
from netbox.api.serializers import NetBoxModelSerializer, WritableNestedSerializer
17-
from utilities.api import get_serializer_for_model
1817
from vpn.api.serializers_.l2vpn import L2VPNTerminationSerializer
1918
from wireless.api.serializers_.nested import NestedWirelessLinkSerializer
2019
from wireless.api.serializers_.wirelesslans import WirelessLANSerializer
@@ -394,7 +393,7 @@ class InventoryItemSerializer(NetBoxModelSerializer):
394393
required=False,
395394
allow_null=True
396395
)
397-
component = serializers.SerializerMethodField(read_only=True, allow_null=True)
396+
component = GFKSerializerField(read_only=True)
398397
_depth = serializers.IntegerField(source='level', read_only=True)
399398
status = ChoiceField(choices=InventoryItemStatusChoices, required=False)
400399

@@ -406,11 +405,3 @@ class Meta:
406405
'component_id', 'component', 'tags', 'custom_fields', 'created', 'last_updated', '_depth',
407406
]
408407
brief_fields = ('id', 'url', 'display', 'device', 'name', 'description', '_depth')
409-
410-
@extend_schema_field(serializers.JSONField(allow_null=True))
411-
def get_component(self, obj):
412-
if obj.component is None:
413-
return None
414-
serializer = get_serializer_for_model(obj.component)
415-
context = {'request': self.context['request']}
416-
return serializer(obj.component, nested=True, context=context).data

netbox/dcim/api/serializers_/devices.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
from extras.api.serializers_.configtemplates import ConfigTemplateSerializer
1212
from ipam.api.serializers_.ip import IPAddressSerializer
1313
from netbox.api.fields import ChoiceField, ContentTypeField, RelatedObjectCountField
14+
from netbox.api.gfk_fields import GFKSerializerField
1415
from netbox.api.serializers import PrimaryModelSerializer
1516
from tenancy.api.serializers_.tenants import TenantSerializer
16-
from utilities.api import get_serializer_for_model
1717
from virtualization.api.serializers_.clusters import ClusterSerializer
1818
from .devicetypes import *
1919
from .nested import NestedDeviceBaySerializer, NestedDeviceSerializer, NestedModuleBaySerializer
@@ -165,7 +165,7 @@ class MACAddressSerializer(PrimaryModelSerializer):
165165
required=False,
166166
allow_null=True
167167
)
168-
assigned_object = serializers.SerializerMethodField(read_only=True)
168+
assigned_object = GFKSerializerField(read_only=True)
169169

170170
class Meta:
171171
model = MACAddress
@@ -174,11 +174,3 @@ class Meta:
174174
'assigned_object', 'description', 'owner', 'comments', 'tags', 'custom_fields', 'created', 'last_updated',
175175
]
176176
brief_fields = ('id', 'url', 'display', 'mac_address', 'description')
177-
178-
@extend_schema_field(serializers.JSONField(allow_null=True))
179-
def get_assigned_object(self, obj):
180-
if obj.assigned_object is None:
181-
return None
182-
serializer = get_serializer_for_model(obj.assigned_object)
183-
context = {'request': self.context['request']}
184-
return serializer(obj.assigned_object, nested=True, context=context).data

netbox/dcim/api/serializers_/devicetype_components.py

Lines changed: 2 additions & 11 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 dcim.choices import *
@@ -9,8 +8,8 @@
98
InventoryItemTemplate, ModuleBayTemplate, PowerOutletTemplate, PowerPortTemplate, RearPortTemplate,
109
)
1110
from netbox.api.fields import ChoiceField, ContentTypeField
11+
from netbox.api.gfk_fields import GFKSerializerField
1212
from netbox.api.serializers import ChangeLogMessageSerializer, ValidatedModelSerializer
13-
from utilities.api import get_serializer_for_model
1413
from wireless.choices import *
1514
from .devicetypes import DeviceTypeSerializer, ModuleTypeSerializer
1615
from .manufacturers import ManufacturerSerializer
@@ -313,7 +312,7 @@ class InventoryItemTemplateSerializer(ComponentTemplateSerializer):
313312
required=False,
314313
allow_null=True
315314
)
316-
component = serializers.SerializerMethodField(read_only=True, allow_null=True)
315+
component = GFKSerializerField(read_only=True)
317316
_depth = serializers.IntegerField(source='level', read_only=True)
318317

319318
class Meta:
@@ -324,11 +323,3 @@ class Meta:
324323
'_depth',
325324
]
326325
brief_fields = ('id', 'url', 'display', 'name', 'description', '_depth')
327-
328-
@extend_schema_field(serializers.JSONField(allow_null=True))
329-
def get_component(self, obj):
330-
if obj.component is None:
331-
return None
332-
serializer = get_serializer_for_model(obj.component)
333-
context = {'request': self.context['request']}
334-
return serializer(obj.component, nested=True, context=context).data

netbox/extras/api/serializers_/attachments.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from django.core.exceptions import ObjectDoesNotExist
2-
from drf_spectacular.utils import extend_schema_field
32
from rest_framework import serializers
43

54
from core.models import ObjectType
65
from extras.models import ImageAttachment
76
from netbox.api.fields import ContentTypeField
7+
from netbox.api.gfk_fields import GFKSerializerField
88
from netbox.api.serializers import ValidatedModelSerializer
9-
from utilities.api import get_serializer_for_model
109

1110
__all__ = (
1211
'ImageAttachmentSerializer',
@@ -17,7 +16,7 @@ class ImageAttachmentSerializer(ValidatedModelSerializer):
1716
object_type = ContentTypeField(
1817
queryset=ObjectType.objects.all()
1918
)
20-
parent = serializers.SerializerMethodField(read_only=True)
19+
parent = GFKSerializerField(read_only=True)
2120
image_width = serializers.IntegerField(read_only=True)
2221
image_height = serializers.IntegerField(read_only=True)
2322

@@ -43,9 +42,3 @@ def validate(self, data):
4342
super().validate(data)
4443

4544
return data
46-
47-
@extend_schema_field(serializers.JSONField(allow_null=True))
48-
def get_parent(self, obj):
49-
serializer = get_serializer_for_model(obj.parent)
50-
context = {'request': self.context['request']}
51-
return serializer(obj.parent, nested=True, context=context).data

0 commit comments

Comments
 (0)