Passing model class to function changes behavior
Question: The first set of python code properly imports an entire CSV file. However, if
I try to pass the model ZipMHA as a parameter, it only imports the first line
of the CSV file. Can anybody explain this change in behavior when passing the
model into the function?

    
    
    import csv
    from bah_api.models import withDependents, withOutDependents, ZipMHA
    
    # Populate CSV file into model
    def LoadCSV(file_location, delim):
        f = open(file_location)
        csv_f = csv.reader(f, delimiter=delim)
        for row in csv_f:
            i = 1
            # create a model instance   
            target_model = ZipMHA()
            #loop through the rows
            for y in row:
                setattr(target_model, target_model._meta.fields[i].name, y)
                i += 1
            # save each row
            target_model.save()
        f.close()
    
    LoadCSV("BAH2015/sorted_zipmha15.txt", ' ')
    

Model passed as parameter (only reads first line):

    
    
    # Populate CSV file into model
    def LoadCSV(file_location, my_model, delim):
        f = open(file_location)
        csv_f = csv.reader(f, delimiter=delim)
        for row in csv_f:
            i = 1
            # create a model instance   
            target_model = my_model
            #loop through the rows
            for y in row:
                setattr(target_model, target_model._meta.fields[i].name, y)
                i += 1
            # save each row
            target_model.save()
        f.close()
    
    LoadCSV("BAH2015/sorted_zipmha15.txt", ZipMHA(), ' ')
    


Answer: You are passing the model instance instead of the model class. Creation of the
`target_model` instance should be like this:

    
    
    target_model = my_model() # note the round brackets
    

And call the function without the brackets after the `ZipMHA` class name:

    
    
    LoadCSV("BAH2015/sorted_zipmha15.txt", ZipMHA, ' ')
    

