You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

44 lines
1.8 KiB

from django.core.management.base import BaseCommand
from django.db import transaction
from apps.hadis.models import Hadis, HadisCorrection, TransmitterOriginalText
class Command(BaseCommand):
help = 'Refreshes slugs and share_links by re-saving all existing instances of Hadis models.'
def handle(self, *args, **options):
models_to_refresh = [
(Hadis, "Hadis"),
(HadisCorrection, "Hadis Correction"),
(TransmitterOriginalText, "Transmitter Original Text"),
]
for model_class, name in models_to_refresh:
self.stdout.write(self.style.WARNING(f'--- Processing {name} ---'))
instances = model_class.objects.all()
count = instances.count()
if count == 0:
self.stdout.write(f"No {name} instances found.")
continue
processed = 0
# Using transaction.atomic to ensure database integrity
with transaction.atomic():
for instance in instances:
try:
# This triggers your updated .save() logic
instance.save()
processed += 1
if processed % 100 == 0:
self.stdout.write(f"Updated {processed}/{count} {name}...")
except Exception as e:
self.stdout.write(self.style.ERROR(
f"Error saving {name} ID {instance.id}: {str(e)}"
))
self.stdout.write(self.style.SUCCESS(f'Successfully refreshed {processed} {name} instances.'))
self.stdout.write(self.style.SUCCESS('--- All models refreshed successfully ---'))