Skip to content

Commit ea1d9e9

Browse files
mn-ramclaude
andcommitted
[fix] Avoid N+1 queries when deleting VpnClient instances openwisp#1221
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 156a756 commit ea1d9e9

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

openwisp_controller/config/api/serializers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ def _update_config(self, device, config_data):
202202
with transaction.atomic():
203203
vpn_list = config.templates.filter(type="vpn").values_list("vpn")
204204
if vpn_list:
205-
for vpnclient in config.vpnclient_set.exclude(
205+
for vpnclient in config.vpnclient_set.select_related(
206+
'vpn', 'cert', 'ip'
207+
).exclude(
206208
vpn__in=vpn_list
207209
).iterator():
208210
vpnclient.delete()

openwisp_controller/config/base/config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,9 @@ def manage_vpn_clients(cls, action, instance, pk_set, **kwargs):
339339
# If the device is deactivated or in the process of deactivating, then
340340
# delete all vpn clients and return.
341341
with transaction.atomic():
342-
for vpnclient in instance.vpnclient_set.all().iterator():
342+
for vpnclient in instance.vpnclient_set.select_related(
343+
'vpn', 'cert', 'ip'
344+
).iterator():
343345
vpnclient.delete()
344346
return
345347

@@ -373,7 +375,9 @@ def manage_vpn_clients(cls, action, instance, pk_set, **kwargs):
373375
# ones, have been fully added. At that point, we can identify and
374376
# delete VpnClient objects not linked to the final template set.
375377
with transaction.atomic():
376-
for vpnclient in instance.vpnclient_set.exclude(
378+
for vpnclient in instance.vpnclient_set.select_related(
379+
'vpn', 'cert', 'ip'
380+
).exclude(
377381
template_id__in=instance.templates.values_list("id", flat=True)
378382
).iterator():
379383
vpnclient.delete()

0 commit comments

Comments
 (0)