I am developing an app in Django.
I have a file model (let's say my_file_model
) like this:
class my_file_model(models.Model):
file = models.FileField(upload_to='myDirectory/', blank=False, null=False)
in which are stored excel sheets like this:
(fixed columns number, variable rows number)
And I have an empty model (let's say output_model
) like this:
class output_model(models.Model):
field_A=models.CharField(max_length=256, blank=False, null=False)
field_B=models.CharField(max_length=256, blank=False, null=False)
field_C=models.CharField(max_length=256, blank=False, null=False)
I want to read line by line the data inside the excel file and save it inside my output_model,
so that I get something like this:
output_model:
object 1:
field_A: a
field_B: b
field_C: c
object 2:
field_A: d
field_B: e
field_C: f
object 3:
field_A: g
field_B: h
field_C: i
...
...
How can I do it?
Here is my script as far as I could write it:
def pour_entire_my_file_model():
import pandas as pd
from .models import my_file_model, output_model
all_files = my_file_model.objects.all()
for file_element in all_files:
# I don't know how to access the excel file contents.
for row in file_element:
# I don't know how to access a certain excel file cell value of a file that is in my model.
output_model.objects.create(field_A=cell(row,1)) # it's a
output_model.objects.create(field_B=cell(row,2)) # it's b
output_model.objects.create(field_C=cell(row,3)) # it's c