# hadis/management/commands/dump_with_encoding.py from django.core.management.base import BaseCommand from django.core.management import call_command import sys import os class Command(BaseCommand): help = 'Dump data with proper UTF-8 encoding' def add_arguments(self, parser): parser.add_argument( 'models', nargs='+', type=str, help='Models to dump (e.g., hadis.HadisCategory hadis.HadisSect)' ) parser.add_argument( '--output', type=str, default='fixture.json', help='Output file path' ) parser.add_argument( '--indent', type=int, default=2, help='JSON indent level' ) def handle(self, *args, **options): models = options['models'] output_file = options['output'] indent = options['indent'] # Ensure directory exists os.makedirs(os.path.dirname(output_file) or '.', exist_ok=True) self.stdout.write(f'Dumping models: {", ".join(models)}...') try: # Force UTF-8 encoding with open(output_file, 'w', encoding='utf-8') as f: call_command( 'dumpdata', *models, indent=indent, stdout=f ) self.stdout.write( self.style.SUCCESS(f'✓ Successfully dumped to {output_file}') ) except Exception as e: self.stdout.write(self.style.ERROR(f'✗ Error: {str(e)}'))