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)



On Friday, July 10, 2020 at 11:55:53 AM UTC+5:30 Ashutosh Mishra wrote:

> 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
>
> On Fri, Jul 10, 2020, 11:07 AM Jakob Damgaard Møller <[email protected]> 
> wrote:
>
>> 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 <[email protected]> 
>> 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 [email protected].
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django-rest-framework/49add8fa-cce2-44fd-b6b2-057a93c8c840n%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/django-rest-framework/49add8fa-cce2-44fd-b6b2-057a93c8c840n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
>>
>> -- 
>> Jakob Damgaard Olsen
>> Tlf: 24613112
>>
>> -- 
>>
> 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 
>> [email protected].
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-rest-framework/CAMmJSsEnFBKka8JioxLfk45K9fahu-kU4RDe9Newy8mKRKeDhQ%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/django-rest-framework/CAMmJSsEnFBKka8JioxLfk45K9fahu-kU4RDe9Newy8mKRKeDhQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/97a1f1a1-eb93-4d1a-a381-c125edd0af27n%40googlegroups.com.

Reply via email to