Friday, April 26, 2013

Re: Upload data in csv file to bd

On Fri, Apr 26, 2013 at 1:56 PM, Hélio Miranda <helio13m@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

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