Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- """
- Load data from a CSV file into Django.
- Note: This is a bare-bones example, for the sake
- of clarity.
- """
- import csv
- os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
- from myproject.myapp.models import Item
- #iterate through the items in the CSV file,
- #updating or inserting as necessary
- INPUT_FILE = '/tmp/some_data.csv'
- input = csv.DictReader(open(INPUT_FILE))
- for line in input:
- id = int(line['Internal ID'])
- #don't duplicate a product, or raise
- #an integrity error e.g., add a new product
- #or update it if it already exists
- current, just_created = Item.objects.get_or_create(internal_id = id)
- current.short_desc = line['Description']
- current.weight = float(line['Weight'])
- try:
- current.save()
- except Exception, ex:
- print ex
- break
Advertisement
Add Comment
Please, Sign In to add comment