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

13745 device type migration (#13747)

* 13745 update migrations to use batch_size

* 13745 update migrations to use subquery update

* 13745 refactor and update other counter migrations
This commit is contained in:
Arthur Hanson
2023-09-18 06:59:26 -07:00
committed by GitHub
parent 3d1f668235
commit b0541be107
6 changed files with 47 additions and 104 deletions

View File

@@ -1,5 +1,5 @@
from django.apps import apps
from django.db.models import F
from django.db.models import F, Count, OuterRef, Subquery
from django.db.models.signals import post_delete, post_save
from netbox.registry import registry
@@ -23,6 +23,24 @@ def update_counter(model, pk, counter_name, value):
)
def update_counts(model, field_name, related_query):
"""
Perform a bulk update for the given model and counter field. For example,
update_counts(Device, '_interface_count', 'interfaces')
will effectively set
Device.objects.update(_interface_count=Count('interfaces'))
"""
subquery = Subquery(
model.objects.filter(pk=OuterRef('pk')).annotate(_count=Count(related_query)).values('_count')
)
return model.objects.update(**{
field_name: subquery
})
#
# Signal handlers
#