# backend/apps/hadis/management/commands/find_orphan_volumes.py from django.core.management.base import BaseCommand from apps.hadis.models import BookReference class Command(BaseCommand): help = 'Prints the slugs of BookReferences that have volumes but no associated editions.' def handle(self, *args, **options): # فیلتر کردن رفرنس‌هایی که تعداد ادیشن‌های آن‌ها صفر است اما تعداد ولوم‌های آن‌ها بیشتر از صفر است orphaned_references = BookReference.objects.filter( editions__isnull=True, volumes__isnull=False ).distinct() if not orphaned_references.exists(): self.stdout.write(self.style.SUCCESS("✅ No orphaned volumes found. All volumes have associated editions.")) return self.stdout.write(self.style.WARNING("Found the following BookReference slugs (have volumes, no edition):")) self.stdout.write("-" * 40) for book in orphaned_references: self.stdout.write(f"Slug: {book.slug} | ID: {book.id}") self.stdout.write("-" * 40) self.stdout.write(self.style.SUCCESS(f"Total found: {orphaned_references.count()}"))