perf: 修改..

pull/8605/head
ibuler 2022-04-12 19:24:59 +08:00
parent 9f927f9703
commit 5101aae5ae
3 changed files with 33 additions and 7 deletions

View File

@ -33,7 +33,6 @@ class Migration(migrations.Migration):
('created_by', models.CharField(blank=True, max_length=32, null=True, verbose_name='Created by')),
('date_created', models.DateTimeField(auto_now_add=True, null=True, verbose_name='Date created')),
('date_updated', models.DateTimeField(auto_now=True, verbose_name='Date updated')),
('host', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='assets.host', related_name='device_info', verbose_name='Host')),
],
options={
'verbose_name': 'DeviceInfo',

View File

@ -1,7 +1,7 @@
# Generated by Django 3.1.14 on 2022-04-02 08:27
from django.utils import timezone
from django.db import migrations
from django.db import migrations, models
def migrate_hardware(apps, *args):
@ -23,10 +23,10 @@ def migrate_hardware(apps, *args):
asset_ids = [h.asset_ptr_id for h in hosts]
assets = asset_model.objects.filter(id__in=asset_ids)
asset_mapper = {a.id: a for a in assets}
if not hosts:
break
hardware_list = []
for host in hosts:
hardware = hardware_model()
asset = asset_mapper[host.asset_ptr_id]
@ -34,10 +34,9 @@ def migrate_hardware(apps, *args):
hardware.date_updated = timezone.now()
for name in fields:
setattr(hardware, name, getattr(asset, name))
hardware_list.append(hardware)
hardware_model.objects.bulk_create(hardware_list, ignore_conflicts=True)
created += len(hardware_list)
hardware.save()
host.device_info = hardware
host.save()
class Migration(migrations.Migration):
@ -47,5 +46,10 @@ class Migration(migrations.Migration):
]
operations = [
migrations.AddField(
model_name='host',
name='device_info',
field=models.ForeignKey(null=True, on_delete=models.deletion.SET_NULL, to='assets.deviceinfo', verbose_name='Device info'),
),
migrations.RunPython(migrate_hardware)
]

View File

@ -0,0 +1,23 @@
# Generated by Django 3.1.14 on 2022-04-12 11:07
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('assets', '0097_auto_20220407_1726'),
]
operations = [
migrations.RemoveField(
model_name='deviceinfo',
name='host',
),
migrations.AddField(
model_name='host',
name='device_info',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='assets.deviceinfo', verbose_name='Device info'),
),
]