Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions core/management/commands/load_data.py
Original file line number Diff line number Diff line change
@@ -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')
)