-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimport_csv.py
More file actions
28 lines (24 loc) · 875 Bytes
/
import_csv.py
File metadata and controls
28 lines (24 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import csv
from django.core.management import BaseCommand
from django.utils import timezone
from exercise.models import Theme
class Command(BaseCommand):
help = "Loads themes of exercises from CSV"
def add_arguments(self, parser):
parser.add_argument("file_path", type=str)
def handle(self, *args, **options):
start_time = timezone.now()
file_path = options["file_path"]
with open(file_path, "r") as csv_file:
data = csv.reader(csv_file, delimiter=",")
for row in data[1:]:
Theme.objects.create(
id = row[0],
teacher = row[1]
)
end_time = timezone.now()
self.stdout.write(
self.style.SUCCESS(
f"Loading CSV took: {(end_time-start_time).total_seconds()} seconds."
)
)