Features: 1) Add management command to delete products with no associated order in batches.

Fixes: 1) Remove duplicate field 'is_active' in ProductAdmin.general_fields.

Extra: 1) Add transaction management for batch deletions; 2) Improve command output with success message.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-06-29 22:07:59 +03:00
parent e64ddddf9b
commit e932d81572
2 changed files with 25 additions and 1 deletions

View file

@ -256,7 +256,7 @@ class ProductAdmin(FieldsetsMixin, BasicModelAdmin):
autocomplete_fields = ("category", "brand", "tags")
inlines = [AttributeValueInline, ProductImageInline, StockInline]
general_fields = ["is_active", "name", "partnumber", "is_active", "is_digital"]
general_fields = ["is_active", "name", "partnumber", "is_digital"]
relation_fields = ["category", "brand", "tags"]

View file

@ -0,0 +1,24 @@
from django.core.management.base import BaseCommand
from django.db import transaction
from core.models import Product
CHUNK_SIZE = 5000
class Command(BaseCommand):
help = "Delete Product rows with no OrderProduct, in batches"
def handle(self, *args, **options):
while True:
batch_ids = list(
Product.objects
.filter(orderproduct__isnull=True)
.values_list('pk', flat=True)[:CHUNK_SIZE]
)
if not batch_ids:
break
with transaction.atomic():
Product.objects.filter(pk__in=batch_ids).delete()
self.stdout.write(f"Deleted {len(batch_ids)} products…")
self.stdout.write("✅ All unordered products removed.")