check genre and insert

18 views
Skip to first unread message

Hélio Miranda

unread,
Apr 26, 2013, 1:04:42 PM4/26/13
to django...@googlegroups.com
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 /
'Comedy'

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] = obj.id
                        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
                          
                        else:
                                report.MovieTitle = line["MovieTitle"]
                                print report.MovieTitle
                        #report.IdGenre = line["genres[obj.GenreType]"]
                                report.MovieYear = line["MovieYear"]
                                report.MovieDuration = line["MovieDuration"]
                                report.save()
                #file.close()
        return render_to_response('index.html', {},
                                                          context_instance=RequestContext(request))

Hélio Miranda

unread,
Apr 27, 2013, 5:08:23 AM4/27/13
to django...@googlegroups.com
Made the proposed change, but what happens is that when will insert the movie with a genre that already exists to get the id of that genre, gives the following error: ObjectId ('517b15658774a7177cb1280d ') as the id that comes from this comes mongodb form.

When the genre does not exist and must enter the new genus, is not to enter.

Does anyone know what I'm doing wrong?
The code is here:
Código (Python):
def csv_upload(request):
    if request.method == 'POST':
            gen = Genre.objects.all()
            genres = dict(Genre.objects.all().values_list('GenreType', 'id'))
            print genres

            for obj in gen:
                    genres[obj.GenreType] = obj.id
                    print genres[obj.GenreType]
            file = csv.DictReader(request.FILES['file'], delimiter=',', quotechar='"')
            for line in file:                      
                    report = Movie()
                    if not line["IdGenre"in genres:
                            print line["IdGenre"]

                            rep = Genre()
                            rep.GenreType = line["IdGenre"]
                            #print rep.GenreType

                    else:
                            report.MovieTitle = line["MovieTitle"]
                            print report.MovieTitle
                            report.IdGenre = line[genres[obj.GenreType]]

                            report.MovieYear = line["MovieYear"]
                            report.MovieDuration = line["MovieDuration"]
                            report.save()

Felipe Coelho

unread,
Apr 27, 2013, 5:08:55 PM4/27/13
to Django users
2013/4/27 Hélio Miranda <heli...@gmail.com>
Made the proposed change, but what happens is that when will insert the movie with a genre that already exists to get the id of that genre, gives the following error: ObjectId ('517b15658774a7177cb1280d ') as the id that comes from this comes mongodb form.

When the genre does not exist and must enter the new genus, is not to enter.

Does anyone know what I'm doing wrong?
 (snip)

I don't know what your issue really is, but here's a quick debugging tip: add the following lines somewhere in your view:

import code
code.interact(local=locals())

A Python interpreter will open in the same terminal where you're running the development server and you'll be able to do anything you want, inspect variables, query the database, you name it. Quit the interpreter (CTRL+D, don't use exit()) and the view will finish processing normally. It should help you finding where your assumptions are failing
Reply all
Reply to author
Forward
0 new messages