Я создаю API для получения данных из модели django для таблицы Excel.
При этом я получаю ошибку, указанную в заголовке.
models.py
class Task(models.Model):
Id=models.IntegerField()
Name=models.CharField(max_length=50,null=False,blank=True)
Image1=models.FileField(blank=True, default="",
upload_to="media/images",null=True)
Image2=models.FileField(blank=True, default="",
upload_to="media/images",null=True)
Date=models.DateField(null=True,blank=True)
def __str__(self):
return str(self.Name)
views.py
class TaskViewSet(viewsets.ViewSet):
def list(self,request):
try:
queryset=Task.objects.all()
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename="users.xls'
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "Your Title"
row_num=0
columns=['Id','Name','Image1','Image2','Date']
for col_num in range(len(columns)):
c = ws.cell(row=row_num + 1, column=col_num + 1)
c.value = columns[col_num][0]
ws.column_dimensions[get_column_letter(col_num + 1)].width = columns[col_num][1]
for obj in queryset:
row_num += 1
row = [
obj.Id,
obj.Name,
obj.Image1,
obj.Image2,
obj.Date,
]
for col_num in range(len(row)):
c = ws.cell(row=row_num + 1, column=col_num + 1)
c.value = row[col_num]
wb.save(response)
return response
except Exception as error:
traceback.print_exc()
return Response({"message": str(error), "success": False}, status=status.HTTP_200_OK)
Traceback:
Traceback (most recent call last):
File "/home/ashu/Desktop/django/task/office/views.py", line 54, in list
ws.column_dimensions[get_column_letter(col_num + 1)].width = columns[col_num][1]
File "/home/ashu/Desktop/django/venv/lib/python3.6/site-packages/openpyxl/descriptors/base.py", line 67, in __set__
value = _convert(self.expected_type, value)
File "/home/ashu/Desktop/django/venv/lib/python3.6/site-packages/openpyxl/descriptors/base.py", line 57, in _convert
raise TypeError('expected ' + str(expected_type))
TypeError: expected <class 'float'>