import random from django.core.management.base import BaseCommand from django.core.cache import cache from apps.hadis.models import Transmitters class Command(BaseCommand): help = "Randomly split all existing Transmitters 50/50 between male and female for testing." def handle(self, *args, **options): transmitters = list(Transmitters.objects.all().order_by("id")) total = len(transmitters) if total == 0: self.stdout.write(self.style.WARNING("No transmitters found in the database.")) return random.shuffle(transmitters) half = total // 2 female_ids = [t.id for t in transmitters[:half]] male_ids = [t.id for t in transmitters[half:]] female_updated = Transmitters.objects.filter(id__in=female_ids).update( gender="female" ) male_updated = Transmitters.objects.filter(id__in=male_ids).update( gender="male" ) # Clear cache so XMind and sync endpoints reflect updated genders immediately try: cache.clear() except Exception: pass self.stdout.write( self.style.SUCCESS( f"Successfully updated {total} transmitters: {male_updated} male, {female_updated} female." ) )