i have created an api through which i can download the excel file with all 
fields except images.I am posting my models and views

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=HttpResponse(content_type='application/ms-excel')

#response['Content-Disposition'] = 'attachment; filename="users.xls'
response['Content-Disposition']='attachment; filename="users.xls"'

wb=openpyxl.Workbook()
ws=wb.active

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]
for obj in queryset:
row_num+=1
row = [
obj.Id,
obj.Name,
obj.Image1.url,
obj.Image2.url,
str(obj.Date),
]
print(type(row))

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)


On Friday, July 10, 2020 at 5:48:36 PM UTC+5:30 ajoeiam wrote:

> On Thu, Jul 9, 2020 at 10:02 PM Ashutosh Mishra <ashutosh...@gmail.com> 
> wrote:
>
>> I am creating an api to get data and images from django model to excel 
>> sheet ,how can i do that,someone please help me.  
>>
>>
>> What have you tried so far?
>
> Regards 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c1092b58-9d91-4d9c-8654-54df883c5172n%40googlegroups.com.

Reply via email to