Features: 1) Add exception handling to product deletion process in delete_never_ordered_products command;

Fixes: 1) Ensure command continues execution if an error occurs in batch deletion;
Extra: 1) Minor formatting adjustment by removing an unnecessary blank line.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-07-05 02:44:27 +03:00
parent 8f5fd2582c
commit db0d64bfd2

View file

@ -1,10 +1,10 @@
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.db import transaction
from core.models import AttributeValue, Product, ProductImage from core.models import AttributeValue, Product, ProductImage
CHUNK_SIZE = 5000 CHUNK_SIZE = 5000
class Command(BaseCommand): class Command(BaseCommand):
help = "Delete Product rows with no OrderProduct, in batches" help = "Delete Product rows with no OrderProduct, in batches"
@ -15,10 +15,12 @@ class Command(BaseCommand):
) )
if not batch_ids: if not batch_ids:
break break
with transaction.atomic(): try:
AttributeValue.objects.filter(product_id__in=batch_ids).delete() AttributeValue.objects.filter(product_id__in=batch_ids).delete()
ProductImage.objects.filter(product_id__in=batch_ids).delete() ProductImage.objects.filter(product_id__in=batch_ids).delete()
Product.objects.filter(pk__in=batch_ids).delete() Product.objects.filter(pk__in=batch_ids).delete()
except Exception:
continue
self.stdout.write(f"Deleted {len(batch_ids)} products…") self.stdout.write(f"Deleted {len(batch_ids)} products…")
self.stdout.write("✅ All unordered products removed.") self.stdout.write("✅ All unordered products removed.")