From b89f7f1c54e5463aaa2fa13d33263e5c61285c5f Mon Sep 17 00:00:00 2001 From: Horilla Date: Fri, 15 Nov 2024 16:41:33 +0530 Subject: [PATCH] [FIX] BASE: Custom command for triggering save for the model - for attendance work record not display issue --- base/management/commands/trigger_save.py | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 base/management/commands/trigger_save.py diff --git a/base/management/commands/trigger_save.py b/base/management/commands/trigger_save.py new file mode 100644 index 000000000..6ea837403 --- /dev/null +++ b/base/management/commands/trigger_save.py @@ -0,0 +1,28 @@ + +from django.core.management.base import BaseCommand, CommandError +from django.apps import apps + +class Command(BaseCommand): + help = 'Save all instances of the specified model' + + def add_arguments(self, parser): + parser.add_argument('app_label', type=str, help='App label of the model (e.g., attendance)') + parser.add_argument('model_name', type=str, help='Name of the model to save instances for (e.g., Attendance)') + + def handle(self, *args, **kwargs): + app_label = kwargs['app_label'] + model_name = kwargs['model_name'] + + try: + model = apps.get_model(app_label, model_name) + except LookupError: + raise CommandError(f"Model '{model_name}' not found in the app '{app_label}'.") + + try: + instances = model.objects.all() + print(f"Saving {len(instances)} instances of '{model_name}' in app '{app_label}'. Please wait.....") + for instance in instances: + instance.save() + self.stdout.write(self.style.SUCCESS(f"All instances of '{model_name}' in app '{app_label}' have been saved.")) + except Exception as e: + raise CommandError(f"An error occurred while saving instances of '{model_name}' in app '{app_label}': {e}")