Hi everyone,
Can anyone tell me how to use a csv file stored in a django model?
I’d like to access certain columns using a python script called in views. However, when I try to access the file as follows, I get the error that the “FieldFile object is not subscriptable”.
#access last instance of model
my_model = ArrayCsv.objects.all().order_by('-id')[0]
my_csv = my_model.csv_file
#try action on csv file
print(my_csv[0])
Alternatively, when I try the following, I get an error: “expected str, bytes or os.PathLike object, not FieldFile”
#access last instance of model
my_model = ArrayCsv.objects.all().order_by('-id')[0]
my_csv = my_model.csv_file
#try to open csv file like a normal python script
with open(my_csv, newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
print(', '.join(row))
Is there an established way to do this? Many thanks