Friday, April 26, 2013

Re: check genre and insert

Hi. May be you need

if not line["IdGenre"] in genres: 

instead?


On Friday, April 26, 2013 9:04:42 PM UTC+4, Hélio Miranda wrote:
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))

--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment