I have a custom command to populate Django objects with JSON data that is working mostly the way I want, but I'm having a problem with django-taggit fields. After parsing all of my data and outputting it in JSON, the genres look like this in my JSON objects:
"genres": "latin pop, pop, singer-songwriter",
django-taggit takes a comma-separated list of tags, which is what this is. But when I run the below custom command, nothing is put in the genres = TaggableManager()
field from my model.
import json
import dateparser
from django.db import IntegrityError
from django.core.management.base import BaseCommand, CommandError
from concerts.models import Concert, Venue
class Command(BaseCommand):
help = "Load JSON concert data"
def add_arguments(self, parser):
parser.add_argument('concert_file', type=str)
def handle(self, *args, **options):
with open(options['concert_file']) as f:
data = json.load(f)
for concert in data:
try:
venue = Venue.objects.get(name=concert['venue'])
except Venue.DoesNotExist:
print "Can't find venue! " + concert['venue']
pass
del concert['venue']
try:
Concert.objects.create(venue=venue, **concert)
except IntegrityError:
print concert['slug'] + ": This concert already exists!"
pass
# if the slug unique contrainst fails, just pass this concert
How can I get the genres field to populate the way I want?