1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00
netbox-community-netbox/netbox/dcim/migrations/0128_device_location_populate.py

25 lines
585 B
Python
Raw Normal View History

from django.db import migrations
from django.db.models import Subquery, OuterRef
def populate_device_location(apps, schema_editor):
Device = apps.get_model('dcim', 'Device')
Device.objects.filter(rack__isnull=False).update(
location_id=Subquery(
Device.objects.filter(pk=OuterRef('pk')).values('rack__location_id')[:1]
)
)
class Migration(migrations.Migration):
dependencies = [
('dcim', '0127_device_location'),
]
operations = [
migrations.RunPython(
code=populate_device_location
),
]