| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/erp/base/management/commands/ |
Upload File : |
from django.apps import apps
from django.core.management.base import BaseCommand, CommandError
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}"
)