def handle(self, filepath='', *args, **options): print filepath temp = filepath.split('/') lng = temp[0] collection = temp[1] pth = src / lng / collection if lng == 'otr': jsonpath = pth / 'meta' else: jsonpath = pth / 'books.json' jsonfile = open(jsonpath) txt=jsonfile.read() jsonfile.close() data_list = eval(txt) log = open('log','w') print 'items',len(data_list) for d in data_list: if lng == 'otr': bookset = Book.objects.filter(book_file=d['file']) else: bookset = Book.objects.filter(book_file=d['book_file']) #there should be at most one - delete all but one if bookset: for i in range(len(bookset)-1): bookset[i].delete() book = bookset[len(bookset)-1] #now update bookkeys = d.keys() for bookkey in bookkeys: if lng == 'otr': book.a_summary = d['description'] book.cover_img = d['img'] book.a_author = d['author'] book.a_title = d['title'] book.mime_type = d['mime'] book.book_file = d['file'] book.a_collection = collection book.dc_language = lng else: book.bookkey = d[bookkey] if bookkey == 'cover_img': book.cover_img = d['cover_img'] else: if lng == 'otr': book = Book() book.a_summary = d['description'] book.cover_img = d['img'] book.a_author = d['author'] book.a_title = d['title'] book.mime_type = d['mime'] book.book_file = d['file'] book.a_collection = collection book.dc_language = lng else: book = Book(**d) book.save() log.close()