Friday, November 25, 2011

Re: Bulk import of data

On 11/25/2011 03:12 PM, Tom Evans wrote:
[...]
> It's not that tricky, is it?
>
> Read the CSV file, split out the fields.
> Get or create the category
> Get or create the subcategory
> Get or create the product
>
> in code:
>
> import csv
> data = csv.reader(open('/path/to/csv', 'r'), delimiter=';')
> for row in data:
> category = Category.objects.get_or_create(name=row[0])
> sub_category = SubCategory.objects.get_or_create(name=row[1],
> defaults={'parent_category': category})
> product = Product.objects.get_or_create(name=row[2],
> defaults={'sub_category': sub_category})
>
> http://docs.python.org/library/csv.html

Hey Tom, that's very kind of you, so helpful and fast!

I'll use that in my real scenario (which is a bit more complicated).
I'll be back here soon, reporting success :-) or asking for more help!

Cheers!

--
Fabio Natali

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment