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