I populated a Django model with a pandas dataframe.
In order to do this is had I used the to_dict() function in order for JSONField() Django model to accept the data.
This works well. Now what I am trying to do is to update an excel file with these data.
So I am accessing the data in the Django model and trying to use openpyxl to populate an excel with the new data data.
However I am running into problems as I am getting the error saying that it cannot convert to excel.
Is there anyway around this?
My model:
class myvalues(models.Model):
items_list = models.JSONField()
Function that’s trying to populate excel:
values_to_go_into_excel_obtained_from_model = myvalues.objects.values_list(“items_list”, flat=True)
path = file_for_download.my_file.path
wb = load_workbook(path)
ws = wb["Sheet1"]
pd.read_excel(path, engine='openpyxl', sheet_name="Sheet1")
for row, v in enumerate(values_to_go_into_excel_obtained_from_model, 2):
ws.cell(row, 4, v)
wb.save(path)
Any ideas why I am getting the error? Basically what I'm trying to do is:
Step 1: pandas df -> Django model of type Json
Step 2: Access Django model values
Step 3: put values from Django model into excel file
I have step 1 and 2 but cannot get step 3.