I am creating an api to export image from django model to the excel 
sheet.But unable to do that,while doing that geeting only the root path of 
images.Please help it's uergent.

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:
            response=HttpResponse(content_type='application/ms-excel')
            response['Content-Disposition']='attachment; 
filename="users.xls"'

            wb=xlwt.Workbook(encoding='utf-8')
            ws=wb.add_sheet('Tasks', cell_overwrite_ok=True)
            
            
            
            row_num=0
 
            font_style=xlwt.XFStyle()
            font_style.font.bold=True
            columns=['Id','Name','Image1','Image2','Date']
            for col_num in range(len(columns)):
                ws.write(row_num,col_num,columns[col_num],font_style)
            
            font_style=xlwt.XFStyle()
            
data=Task.objects.all()#.values_list('Id','Name','Image1','Image2','Date')
            
            date_format = xlwt.XFStyle()
            date_format.num_format_str = 'yyyy/mm/dd'
            for my_row in data:
                row_num+=1
                
                ws.write(row_num,0,my_row.Id,font_style)
                ws.write(row_num,1,my_row.Name,font_style)
                #ws.write(row_num,2,my_row.Image1.url,font_style)
               
                ws.write(row_num,3,my_row.Image2.url,font_style)
     
                ws.write(row_num,4,str(my_row.Date),font_style)
            wb.save(response)
            print(my_row.Date)
            return response
            
        except Exception as error:
            traceback.print_exc()
            return Response({"message": str(error), "success": False}, 
status=status.HTTP_200_OK)

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/506594bb-95d7-4391-8d34-bc37022b09abn%40googlegroups.com.

Reply via email to