20 lines
689 B
Python
20 lines
689 B
Python
from django.core.management.base import BaseCommand, CommandError
|
|
import argparse
|
|
import csv
|
|
|
|
from library import models
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Imports works from a csv file'
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument('collection', type=int, help="Collection ID")
|
|
parser.add_argument('source', type=argparse.FileType('r'), help="Source CSV")
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
collection = models.Collection.objects.get(pk=options['collection'])
|
|
|
|
reader = csv.DictReader(options['source'])
|
|
for row in reader:
|
|
collection.works.create(name=row['Piece'], composer=row['Composer'], notes=row['Notes']) |