Friday, April 26, 2013

Upload data in csv file to bd

I am uploading a csv file with movies for bd.
Ok, I'm getting it, but my problem is that the movie has genre, but I can not insert the genre, I have to insert the id of the genre, so I'm going to fetch the genres to put a dictionary, GenreType = id.

When I want to see the insert there the genre, if so takes on the genre id and inserts, except inserts the genre in the table of genres.

I'm trying this:
Código (Python):
def csv_upload(request):
        if request.method == 'POST':

          
                genres = {}
                gen = Genre.objects.all()
          
                for obj in gen:
                        print genres[obj.GenreType]
                  
                file = csv.DictReader(request.FILES['file'], delimiter=',', quotechar='"')
                #data = csv.DictReader(request.FILES['file'])
                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()

But it gives the following error: 

KeyError at /

'Comedia'

Comedia is the genre again.  Does anyone know what I'm doing wrong?

--
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