Upload data in csv file to bd

37 views
Skip to first unread message

Hélio Miranda

unread,
Apr 26, 2013, 8:56:37 AM4/26/13
to django...@googlegroups.com
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?

Tom Evans

unread,
Apr 26, 2013, 9:47:13 AM4/26/13
to django...@googlegroups.com
On Fri, Apr 26, 2013 at 1:56 PM, Hélio Miranda <heli...@gmail.com> wrote:
> 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]

genres is still at this point an empty dictionary, so looking up
anything in it will fail. You seem to have missed a step in
constructing your lookup dictionary, try something like this:

genres = dict(Genre.objects.all().values_list('GenreType', 'id'))

PS: When asking for help on here, and you have an exception with some
code, the traceback that follows the exception provides exact log of
how and where the exception was raised. In this case, it would have
pointed out precisely which line the KeyError occurred. In this case,
it was obvious what was wrong, in other cases it may not be so
obvious, so in future please do include the full traceback to assist
the people you are asking to help you.

Cheers

Tom

Hélio Miranda

unread,
Apr 26, 2013, 10:09:44 AM4/26/13
to django...@googlegroups.com
But when I do this:
Código (Python):
for obj in gen:
          genres [obj.GenreType] = obj.id
          print genres [obj.GenreType]

Prints the id of the genre, so it is wrong right?

Now I am not going to achieve is check if the genre that I have the csv is in the dictionary and if it gets the id, but inserts the genre in the table of genres

Horst Jäger

unread,
Apr 29, 2013, 12:08:27 PM4/29/13
to django...@googlegroups.com

Hi folks,

I'm a complete newbie to Django and I have to edit a few lines of code in an existing project.

Of course I can't to so on the production machine.

So I'm trying to migrate the existing project onto a new development server. Django is up and running there and I can create sample projects as described in

https://docs.djangoproject.com/en/dev/intro/tutorial01/

. I have transferred the existing project onto the new development server. I can start it there, but when I try to have a look at its index page I get a runtime error:

File "/opt/djangostack-1.4.5-0/apps/django/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 107, in unregister
raise NotRegistered('The model %s is not registered' % model.__name__)
NotRegistered: The model Consumer is not registered

This error happens in admin.py . This file reads:

from django.contrib import admin

import piston.models

admin.site.unregister(piston.models.Consumer)
admin.site.unregister(piston.models.Nonce)
admin.site.unregister(piston.models.Resource)
admin.site.unregister(piston.models.Token)

Now I wonder where the unregistered module might have been registered. I have searched the whole project tree for the string "Consumer" using grep and this string only occurrs in the file I already mentioned.

Any help would be greately appreciated.

Thanks in andvance

Horst


--
Horst Jäger h.ja...@medienkonzepte.de
Medienkonzepte http://www.medienkonzepte.de/
Schaafenstr. 25, 50676 Köln, Germany
Tel +49 221 93187015 / Fax +49 221 93187029

Shawn Milochik

unread,
Apr 29, 2013, 1:03:38 PM4/29/13
to django...@googlegroups.com
Do you have piston installed in the virtualenv where you're trying to test this?

Check for any "local settings" on the production server that it may be using but which aren't available through the repository.
Reply all
Reply to author
Forward
0 new messages