Skip to content

Closes #19740: Enable recursive nesting for platforms #20047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion docs/models/dcim/platform.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

A platform defines the type of software running on a [device](./device.md) or [virtual machine](../virtualization/virtualmachine.md). This can be helpful to model when it is necessary to distinguish between different versions or feature sets. Note that two devices of the same type may be assigned different platforms: For example, one Juniper MX240 might run Junos 14 while another runs Junos 15.

Platforms may be nested under parents to form a hierarchy. For example, platforms named "Debian" and "RHEL" might both be created under a generic "Linux" parent.

Platforms may optionally be limited by [manufacturer](./manufacturer.md): If a platform is assigned to a particular manufacturer, it can only be assigned to devices with a type belonging to that manufacturer.

The assignment of platforms to devices is an optional feature, and may be disregarded if not desired.
The assignment of platforms to devices and virtual machines is optional.

## Fields

## Parent

!!! "This field was introduced in NetBox v4.4."

The parent platform class to which this platform belongs (optional).

### Name

A human-friendly name for the platform. Must be unique per manufacturer.
Expand Down
9 changes: 9 additions & 0 deletions netbox/dcim/api/serializers_/nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

__all__ = (
'NestedDeviceBaySerializer',
'NestedDeviceRoleSerializer',
'NestedDeviceSerializer',
'NestedInterfaceSerializer',
'NestedInterfaceTemplateSerializer',
'NestedLocationSerializer',
'NestedModuleBaySerializer',
'NestedPlatformSerializer',
'NestedRegionSerializer',
'NestedSiteGroupSerializer',
)
Expand Down Expand Up @@ -102,3 +104,10 @@ class NestedModuleBaySerializer(WritableNestedSerializer):
class Meta:
model = models.ModuleBay
fields = ['id', 'url', 'display_url', 'display', 'name']


class NestedPlatformSerializer(WritableNestedSerializer):

class Meta:
model = models.Platform
fields = ['id', 'url', 'display_url', 'display', 'name']
15 changes: 10 additions & 5 deletions netbox/dcim/api/serializers_/platforms.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from dcim.models import Platform
from extras.api.serializers_.configtemplates import ConfigTemplateSerializer
from netbox.api.fields import RelatedObjectCountField
from netbox.api.serializers import NetBoxModelSerializer
from netbox.api.serializers import NestedGroupModelSerializer
from .manufacturers import ManufacturerSerializer
from .nested import NestedPlatformSerializer

__all__ = (
'PlatformSerializer',
)


class PlatformSerializer(NetBoxModelSerializer):
class PlatformSerializer(NestedGroupModelSerializer):
parent = NestedPlatformSerializer(required=False, allow_null=True, default=None)
manufacturer = ManufacturerSerializer(nested=True, required=False, allow_null=True)
config_template = ConfigTemplateSerializer(nested=True, required=False, allow_null=True, default=None)

Expand All @@ -20,7 +22,10 @@ class PlatformSerializer(NetBoxModelSerializer):
class Meta:
model = Platform
fields = [
'id', 'url', 'display_url', 'display', 'name', 'slug', 'manufacturer', 'config_template', 'description',
'tags', 'custom_fields', 'created', 'last_updated', 'device_count', 'virtualmachine_count',
'id', 'url', 'display_url', 'display', 'parent', 'name', 'slug', 'manufacturer', 'config_template',
'description', 'comments', 'tags', 'custom_fields', 'created', 'last_updated', 'device_count',
'virtualmachine_count', '_depth',
]
brief_fields = ('id', 'url', 'display', 'name', 'slug', 'description', 'device_count', 'virtualmachine_count')
brief_fields = (
'id', 'url', 'display', 'name', 'slug', 'description', 'device_count', 'virtualmachine_count', '_depth',
)
41 changes: 35 additions & 6 deletions netbox/dcim/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,14 +547,17 @@ class DeviceTypeFilterSet(NetBoxModelFilterSet):
to_field_name='slug',
label=_('Manufacturer (slug)'),
)
default_platform_id = django_filters.ModelMultipleChoiceFilter(
default_platform_id = TreeNodeMultipleChoiceFilter(
queryset=Platform.objects.all(),
field_name='default_platform',
lookup_expr='in',
label=_('Default platform (ID)'),
)
default_platform = django_filters.ModelMultipleChoiceFilter(
field_name='default_platform__slug',
default_platform = TreeNodeMultipleChoiceFilter(
queryset=Platform.objects.all(),
field_name='default_platform',
to_field_name='slug',
lookup_expr='in',
label=_('Default platform (slug)'),
)
has_front_image = django_filters.BooleanFilter(
Expand Down Expand Up @@ -979,6 +982,29 @@ class Meta:


class PlatformFilterSet(OrganizationalModelFilterSet):
parent_id = django_filters.ModelMultipleChoiceFilter(
queryset=Platform.objects.all(),
label=_('Immediate parent platform (ID)'),
)
parent = django_filters.ModelMultipleChoiceFilter(
field_name='parent__slug',
queryset=Platform.objects.all(),
to_field_name='slug',
label=_('Immediate parent platform (slug)'),
)
ancestor_id = TreeNodeMultipleChoiceFilter(
queryset=Platform.objects.all(),
field_name='parent',
lookup_expr='in',
label=_('Parent platform (ID)'),
)
ancestor = TreeNodeMultipleChoiceFilter(
queryset=Platform.objects.all(),
field_name='parent',
lookup_expr='in',
to_field_name='slug',
label=_('Parent platform (slug)'),
)
manufacturer_id = django_filters.ModelMultipleChoiceFilter(
field_name='manufacturer',
queryset=Manufacturer.objects.all(),
Expand Down Expand Up @@ -1058,14 +1084,17 @@ class DeviceFilterSet(
queryset=Device.objects.all(),
label=_('Parent Device (ID)'),
)
platform_id = django_filters.ModelMultipleChoiceFilter(
platform_id = TreeNodeMultipleChoiceFilter(
queryset=Platform.objects.all(),
field_name='platform',
lookup_expr='in',
label=_('Platform (ID)'),
)
platform = django_filters.ModelMultipleChoiceFilter(
field_name='platform__slug',
platform = TreeNodeMultipleChoiceFilter(
field_name='platform',
queryset=Platform.objects.all(),
to_field_name='slug',
lookup_expr='in',
label=_('Platform (slug)'),
)
region_id = TreeNodeMultipleChoiceFilter(
Expand Down
10 changes: 8 additions & 2 deletions netbox/dcim/forms/bulk_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,11 @@ class DeviceRoleBulkEditForm(NetBoxModelBulkEditForm):


class PlatformBulkEditForm(NetBoxModelBulkEditForm):
parent = DynamicModelChoiceField(
label=_('Parent'),
queryset=Platform.objects.all(),
required=False,
)
manufacturer = DynamicModelChoiceField(
label=_('Manufacturer'),
queryset=Manufacturer.objects.all(),
Expand All @@ -697,12 +702,13 @@ class PlatformBulkEditForm(NetBoxModelBulkEditForm):
max_length=200,
required=False
)
comments = CommentField()

model = Platform
fieldsets = (
FieldSet('manufacturer', 'config_template', 'description'),
FieldSet('parent', 'manufacturer', 'config_template', 'description'),
)
nullable_fields = ('manufacturer', 'config_template', 'description')
nullable_fields = ('parent', 'manufacturer', 'config_template', 'description', 'comments')


class DeviceBulkEditForm(NetBoxModelBulkEditForm):
Expand Down
12 changes: 11 additions & 1 deletion netbox/dcim/forms/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,16 @@ class Meta:

class PlatformImportForm(NetBoxModelImportForm):
slug = SlugField()
parent = CSVModelChoiceField(
label=_('Parent'),
queryset=Platform.objects.all(),
required=False,
to_field_name='name',
help_text=_('Parent platform'),
error_messages={
'invalid_choice': _('Platform not found.'),
}
)
manufacturer = CSVModelChoiceField(
label=_('Manufacturer'),
queryset=Manufacturer.objects.all(),
Expand All @@ -522,7 +532,7 @@ class PlatformImportForm(NetBoxModelImportForm):
class Meta:
model = Platform
fields = (
'name', 'slug', 'manufacturer', 'config_template', 'description', 'tags',
'name', 'slug', 'parent', 'manufacturer', 'config_template', 'description', 'tags',
)


Expand Down
5 changes: 5 additions & 0 deletions netbox/dcim/forms/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,11 @@ class DeviceRoleFilterForm(NetBoxModelFilterSetForm):
class PlatformFilterForm(NetBoxModelFilterSetForm):
model = Platform
selector_fields = ('filter_id', 'q', 'manufacturer_id')
parent_id = DynamicModelMultipleChoiceField(
queryset=Platform.objects.all(),
required=False,
label=_('Parent')
)
manufacturer_id = DynamicModelMultipleChoiceField(
queryset=Manufacturer.objects.all(),
required=False,
Expand Down
12 changes: 10 additions & 2 deletions netbox/dcim/forms/model_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,11 @@ class Meta:


class PlatformForm(NetBoxModelForm):
parent = DynamicModelChoiceField(
label=_('Parent'),
queryset=Platform.objects.all(),
required=False,
)
manufacturer = DynamicModelChoiceField(
label=_('Manufacturer'),
queryset=Manufacturer.objects.all(),
Expand All @@ -551,15 +556,18 @@ class PlatformForm(NetBoxModelForm):
label=_('Slug'),
max_length=64
)
comments = CommentField()

fieldsets = (
FieldSet('name', 'slug', 'manufacturer', 'config_template', 'description', 'tags', name=_('Platform')),
FieldSet(
'name', 'slug', 'parent', 'manufacturer', 'config_template', 'description', 'tags', name=_('Platform'),
),
)

class Meta:
model = Platform
fields = [
'name', 'slug', 'manufacturer', 'config_template', 'description', 'tags',
'name', 'slug', 'parent', 'manufacturer', 'config_template', 'description', 'comments', 'tags',
]


Expand Down
2 changes: 2 additions & 0 deletions netbox/dcim/graphql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,8 @@ class ModuleTypeType(NetBoxObjectType):
pagination=True
)
class PlatformType(OrganizationalObjectType):
parent: Annotated['PlatformType', strawberry.lazy('dcim.graphql.types')] | None
children: List[Annotated['PlatformType', strawberry.lazy('dcim.graphql.types')]]
manufacturer: Annotated["ManufacturerType", strawberry.lazy('dcim.graphql.types')] | None
config_template: Annotated["ConfigTemplateType", strawberry.lazy('extras.graphql.types')] | None

Expand Down
55 changes: 55 additions & 0 deletions netbox/dcim/migrations/0211_platform_parent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import django.db.models.deletion
import mptt.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dcim', '0210_interface_tx_power_negative'),
]

operations = [
# Add parent & MPTT fields
migrations.AddField(
model_name='platform',
name='parent',
field=mptt.fields.TreeForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name='children',
to='dcim.platform'
),
),
migrations.AddField(
model_name='platform',
name='level',
field=models.PositiveIntegerField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='platform',
name='lft',
field=models.PositiveIntegerField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='platform',
name='rght',
field=models.PositiveIntegerField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='platform',
name='tree_id',
field=models.PositiveIntegerField(db_index=True, default=0, editable=False),
preserve_default=False,
),
# Add comments field
migrations.AddField(
model_name='platform',
name='comments',
field=models.TextField(blank=True),
),
]
29 changes: 29 additions & 0 deletions netbox/dcim/migrations/0212_platform_rebuild.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.db import migrations
import mptt
import mptt.managers


def rebuild_mptt(apps, schema_editor):
"""
Construct the MPTT hierarchy.
"""
Platform = apps.get_model('dcim', 'Platform')
manager = mptt.managers.TreeManager()
manager.model = Platform
mptt.register(Platform)
manager.contribute_to_class(Platform, 'objects')
manager.rebuild()


class Migration(migrations.Migration):

dependencies = [
('dcim', '0211_platform_parent'),
]

operations = [
migrations.RunPython(
code=rebuild_mptt,
reverse_code=migrations.RunPython.noop
),
]
4 changes: 3 additions & 1 deletion netbox/dcim/models/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ class Meta:
verbose_name_plural = _('device roles')


class Platform(OrganizationalModel):
class Platform(NestedGroupModel):
"""
Platform refers to the software or firmware running on a Device. For example, "Cisco IOS-XR" or "Juniper Junos". A
Platform may optionally be associated with a particular Manufacturer.
Expand Down Expand Up @@ -454,6 +454,8 @@ class Platform(OrganizationalModel):
null=True
)

clone_fields = ('parent', 'description')

class Meta:
ordering = ('name',)
verbose_name = _('platform')
Expand Down
2 changes: 1 addition & 1 deletion netbox/dcim/tables/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Meta(NetBoxTable.Meta):
#

class PlatformTable(NetBoxTable):
name = tables.Column(
name = columns.MPTTColumn(
verbose_name=_('Name'),
linkify=True
)
Expand Down
7 changes: 5 additions & 2 deletions netbox/dcim/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,9 @@ def setUpTestData(cls):

class PlatformTest(APIViewTestCases.APIViewTestCase):
model = Platform
brief_fields = ['description', 'device_count', 'display', 'id', 'name', 'slug', 'url', 'virtualmachine_count']
brief_fields = [
'_depth', 'description', 'device_count', 'display', 'id', 'name', 'slug', 'url', 'virtualmachine_count',
]
create_data = [
{
'name': 'Platform 4',
Expand All @@ -1274,7 +1276,8 @@ def setUpTestData(cls):
Platform(name='Platform 2', slug='platform-2'),
Platform(name='Platform 3', slug='platform-3'),
)
Platform.objects.bulk_create(platforms)
for platform in platforms:
platform.save()


class DeviceTest(APIViewTestCases.APIViewTestCase):
Expand Down
Loading