Re: Unable to export images from django model to the excel sheet

29 views
Skip to first unread message
Message has been deleted

Adam Johnson

unread,
Jul 4, 2020, 12:26:28 PM7/4/20
to django-d...@googlegroups.com
Hi!

I think you've found the wrong mailing list for this post. This mailing list is for discussing the development of Django itself, not for support using Django. This means the discussions of bugs and features in Django itself, rather than in your code using it. People on this list are unlikely to answer your support query with their limited time and energy.

For support, please follow the "Getting Help" page: https://docs.djangoproject.com/en/3.0/faq/help/ . This will help you find people who are willing to support you, and to ask your question in a way that makes it easy for them to answer.

Thanks for your understanding and all the best,

Adam

On Sat, 4 Jul 2020 at 13:36, Ashutosh Mishra <ashutosh...@gmail.com> wrote:
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-develop...@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.


--
Adam
Reply all
Reply to author
Forward
0 new messages