From e1ce757778409c9bb09983e2d54654b17ac76ea9 Mon Sep 17 00:00:00 2001 From: Tomdieu Date: Tue, 7 Oct 2025 06:13:23 +0100 Subject: [PATCH] feat: add command to load sample feedback data from JSON file --- core/management/commands/load_data.py | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 core/management/commands/load_data.py diff --git a/core/management/commands/load_data.py b/core/management/commands/load_data.py new file mode 100644 index 0000000..bc8516c --- /dev/null +++ b/core/management/commands/load_data.py @@ -0,0 +1,30 @@ +import json +from django.core.management.base import BaseCommand +from core.models import Topic, Feedback + +class Command(BaseCommand): + help = 'Load sample feedback data from JSON file' + + def add_arguments(self, parser): + parser.add_argument('--file', type=str, default='data/feedbacks.json') + + def handle(self, *args, **options): + try: + with open(options['file'], 'r') as f: + data = json.load(f) + + for item in data: + topic, _ = Topic.objects.get_or_create(name=item['topic']) + Feedback.objects.create( + user_name=item['user_name'], + topic=topic, + content=item['content'] + ) + + self.stdout.write( + self.style.SUCCESS(f'Successfully loaded {len(data)} feedback entries') + ) + except FileNotFoundError: + self.stdout.write( + self.style.ERROR(f'File {options["file"]} not found') + ) \ No newline at end of file