netbox/netbox/dcim/migrations/0204_device_role_rebuild.py
Arthur Hanson 1508e3a770
Fixes #18245: Make DeviceRole Hierarchical (#19008)
Made DeviceRoles hierarchical, had to also change the filtersets for Device, ConfigContext and VirtualMachine to use the TreeNodeMultipleChoiceFilter.

Note: The model was changed to use NestedGroupModel, a side-effect of this is it also adds comments field, but I thought that was better then doing a one-off just for DeviceRole and having to define the fields, validators, etc.. - keeps everything DRY / consistent.

* 18981 Make Device Roles Hierarchical

* 18981 forms, serializer

* 18981 fix tests

* 18981 fix tests

* 18981 fix tests

* 18981 fix tests

* 18981 fix tests

* 18981 fix migration merge

* 18981 fix tests

* 18981 fix filtersets

* 18981 fix tests

* 18981 comments

* 18981 review changes
2025-03-28 14:32:02 -05:00

23 lines
569 B
Python

from django.db import migrations
import mptt
import mptt.managers
def rebuild_mptt(apps, schema_editor):
manager = mptt.managers.TreeManager()
DeviceRole = apps.get_model('dcim', 'DeviceRole')
manager.model = DeviceRole
mptt.register(DeviceRole)
manager.contribute_to_class(DeviceRole, 'objects')
manager.rebuild()
class Migration(migrations.Migration):
dependencies = [
('dcim', '0203_device_role_nested'),
]
operations = [
migrations.RunPython(code=rebuild_mptt, reverse_code=migrations.RunPython.noop),
]