Re: How to get data from django model to excel sheet using openpyxl including images.

23 views
Skip to first unread message
Message has been deleted

Jakob Damgaard Møller

unread,
Jul 10, 2020, 1:37:18 AM7/10/20
to django-res...@googlegroups.com
What have you tried so fare?
Are you able to get the data out of the api as wanted?
Are you able to produce a excel file?

On Fri, Jul 10, 2020 at 5:02 AM 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.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/49add8fa-cce2-44fd-b6b2-057a93c8c840n%40googlegroups.com.


--
Jakob Damgaard Olsen
Tlf: 24613112

Ashutosh Mishra

unread,
Jul 10, 2020, 2:25:53 AM7/10/20
to django-res...@googlegroups.com
Yes ,I have tried,I am getting the excel sheet,everything is correct except not getting the image,getting only the root media folder adress

You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/hmTDx0FgaTA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CAMmJSsEnFBKka8JioxLfk45K9fahu-kU4RDe9Newy8mKRKeDhQ%40mail.gmail.com.

Ashutosh Mishra

unread,
Jul 10, 2020, 2:56:38 AM7/10/20
to Django REST framework
here is my model
Do correct me ,where i am doing the mistake
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)

#my 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)



Reply all
Reply to author
Forward
0 new messages