0

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?

Danny
  • 470
  • 1
  • 4
  • 21

1 Answers1

0

Here's what I figured out in case anyone else ever runs into this problem:

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)
            print concert['slug'] + ": Concert created!"
        except IntegrityError: # if slug already exists
            print concert['slug'] + ": This slug already exists!"
            pass
            # if the slug unique contrainst fails, just pass this concert
        try:
            new_concert = Concert.objects.get(slug=concert['slug'])
            genres = concert['genres'].split(", ")
            for genre in genres:
                new_concert.genres.add(genre)
        except KeyError or AttributeError: # if no genres key exists
            pass

The answer, as you might expect, was in the docs. I checked out the django-taggit documentation, specifically on the API, and then adapted the standard tag adding in my last try/except statement above.

A bonus (for me) of this code is that even if the concert already exists, this code will update the genres field anyway.

Danny
  • 470
  • 1
  • 4
  • 21