Friday, April 26, 2013

check genre and insert

I am having a problem which is as follows:
I have to insert a csv file of movies in bd, but I can not enter the name of the genre of the film directly. I have to insert the id of the genre.

So I'm trying to pick the genres to a dictionary, and then verify that the genre exists, if yes, get the id of that genre and enters the film, if there is the genre enters the genre in the table of genres.

But I am not able to give me the following error:
KeyError at /

This line: if (genres [line ["IdGenre"]] == 0):
What am I doing wrong?

I'm trying this:
Código (Python):
def csv_upload(request):
        if request.method == 'POST':
                genres = {}
                gen = Genre.objects.all()
                for obj in gen:
                        genres[obj.GenreType] =
                        print genres[obj.GenreType]
                file = csv.DictReader(request.FILES['file'], delimiter=',', quotechar='"')

                for line in file:
                        report = Movie()
                        if (genres[line["IdGenre"]] == 0):
                                print genres[line["IdGenre"]]
                                rep = Genre()
                                rep.GenreType = line["IdGenre"]
                                print rep.GenreType
                                report.MovieTitle = line["MovieTitle"]
                                print report.MovieTitle
                        #report.IdGenre = line["genres[obj.GenreType]"]
                                report.MovieYear = line["MovieYear"]
                                report.MovieDuration = line["MovieDuration"]
        return render_to_response('index.html', {},

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

No comments:

Post a Comment