diff --git a/core/admin.py b/core/admin.py index 78f4215a..cec95866 100644 --- a/core/admin.py +++ b/core/admin.py @@ -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"] diff --git a/core/management/commands/delete_never_ordered_products.py b/core/management/commands/delete_never_ordered_products.py new file mode 100644 index 00000000..84b1ba97 --- /dev/null +++ b/core/management/commands/delete_never_ordered_products.py @@ -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.")