Created
February 17, 2014 16:41
-
-
Save arthur-e/9054141 to your computer and use it in GitHub Desktop.
An example Django command (through the django-admin.py or manage.py interface) for loading in generic CSV data; a design pattern that should apply well to just about any delimited data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from django.core.management.base import BaseCommand, CommandError | |
from django.core.exceptions import ValidationError, ObjectDoesNotExist | |
from django.db.models.fields import FieldDoesNotExist | |
from scour_server.nbi.models import Bridge | |
class Command(BaseCommand): | |
args = '<path>' | |
help = '' | |
def handle(self, *args, **options): | |
''' | |
<path> {String} The path to the CSV file | |
''' | |
path = args[0] | |
reader = csv.reader(open(path, 'rb'), delimiter=',', quotechar="'") | |
for line in reader: | |
l = reader.line_num # Lines start numbering at 1, not 0 | |
if line == []: | |
line = reader.next() # Check for empty lines | |
if l == 1: | |
header = line # Get field names | |
line = reader.next() # Move on to the first line of data | |
data_dict = {} | |
for field in header: | |
# Reformat the column names for personal preference/system requirements | |
model_field = field.lower() | |
value = line[header.index(field)] # The value for that field | |
try: | |
Bridge._meta.get_field(model_field) | |
except FieldDoesNotExist: | |
# Skip fields that aren't specified in the model | |
continue | |
# Catch empty values that should be null | |
if len(value) == 0 or value == '_': | |
if Bridge._meta.get_field(model_field).null: | |
data_dict[model_field] = None | |
else: | |
data_dict[model_field] = value | |
inst = Bridge(**data_dict) # Create a model instance | |
try: | |
# Check to see if the record already exists | |
Bridge.objects.get(structure_number_008=inst.structure_number_008) | |
except ObjectDoesNotExist: | |
inst.save() # Save the record to the database only if it doesn't already exist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The example is that of a National Bridge Inventory dataset that needs to be loaded, where the unique identifier is Item #8,
structure_number_008
.