import csv from django.core.management.base import BaseCommand from dbcdapp.models import Song from datetime import timedelta class Command(BaseCommand): help = 'Import data from CSV to Django models' def handle(self, *args, **kwargs): with open('song_data.csv', newline='', encoding='utf-8') as csvfile: csv_reader = csv.reader(csvfile, delimiter=';') for row in csv_reader: duration_str = row[2] duration_parts = duration_str.split(':') duration = timedelta(hours=int(duration_parts[0]), minutes=int(duration_parts[1]), seconds=int(duration_parts[2])) Song.objects.create(song_title=row[0], release_year=int(row[1]), duration=duration) self.stdout.write(self.style.SUCCESS('Data imported successfully'))