I want to fill my django model from a csv. I upload the csv with numpy and then, since my model fields are called like the column headers of the csv, I would like to do something like this:
data=np.genfromtxt("file.csv", delimiter=',', dtype=None, names=True)
columns = data.dtype.names
for i in range(len(data['id'])):
for a in range(1, len(columns)): #1
p=MyModel.objects.filter(columns[a]=data[i][columns[a]])
p.save()
Now, this is quite row, and it doesn't work because I can't pass that columns[a] instead of the field name.
I even tried something like MyModel._meta_fields[a]=data[i][a].
I have tens or even hundreds of fields, sometimes: isn't there an elegant solution that would me spare to write e long list of field names?
Thank you!
If you are trying to create new instances of your model from each row of the data, use the setattr function to set the values:
data=np.genfromtxt("file.csv", delimiter=',', dtype=None, names=True)
columns = data.dtype.names
for i in range(len(data['id'])):
p = MyModel()
for a in range(1, len(columns)): #1
if hasattr(p, columns[a]):
setattr(p, columns[a], data[i][columns[a]])
else:
raise AttributeError("'MyModel' object has no attribute '{0}'".format(columns[a]))
p.save()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With