您好,登錄后才能下訂單哦!
這篇文章主要講解了“Python django導出excel的方法是什么”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Python django導出excel的方法是什么”吧!
web架構:前后端分離,前端使用vue,后端使用django 的rest framework
django版本3.2
django-excel 版本0.0.10
djangorestframework版本3.12.4
界面導出excel數據內容
導入model包含外鍵類型
1.不使用序列化類:需要手動實現外鍵對象轉換為外鍵值
2.使用序化類:
#models.py from django.db import models from django.utils import timezone class MyITtype(models.Model): name = models.CharField(verbose_name="名稱", max_length=128, unique=True) ittype = models.SmallIntegerField(verbose_name="類型") comment = models.TextField(verbose_name="備注", blank=True, default="") def __str__(self): return self.name class Meta: db_table = "MyITtype" verbose_name = "it資產類型" class MyAsset(models.Model): ittype = models.ForeignKey(MyITtype, on_delete=models.SET_NULL, verbose_name="產品類型", null=True) code = models.CharField(verbose_name="資產編碼", max_length=128, unique=True) buytime = models.DateField(verbose_name="入倉時間", default=timezone.now) usetime = models.DateField(verbose_name="分配時間", default=timezone.now) comment = models.TextField(verbose_name="規格說明", blank=True, default="") user = models.CharField(verbose_name="使用人", max_length=128, blank=True, default="") status = models.IntegerField(verbose_name="狀態") def __str__(self): return self.code class Meta: db_table = "opGTITAsset" verbose_name = "it固產"
#asset-export.py from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.serializers import ModelSerializer from .models import MyAsset import django_excel as excel class ITAssetSer(ModelSerializer): class Meta: model = MyAsset fields = '__all__' class ITAssetExport(APIView): """""" use_model = MyAsset queryset = use_model.objects.all() serializer_class = ITAssetSer def post(self, request, *args, **kwargs): try: data = request.data id_list = data.get("data") obj_list = self.queryset.all() # 如果有值,表示導出部分,否則導出全部 if id_list: obj_list = obj_list.filter(id__in=id_list) ser = self.serializer_class(instance=obj_list, many=True) return MakeExcel(obj_list=ser.data, class_name=self.use_model, filename="itasset") except: ret = { "code": 599, "message": "導出數據出錯" } return Response(ret) def get(self, request, *args, **kwargs): self.http_method_not_allowed(request, *args, **kwargs) def MakeExcel(obj_list, class_name, filename): """ 制作excel表 :param ser_list: 內容列表 :param class_name: 類名 :param filename: 文件名 :return: """ meta_fields = class_name._meta.fields name_list = [field.name for field in meta_fields] header_list = [field.verbose_name for field in meta_fields] wdata = [header_list] for obj in obj_list: data = [obj[name] for name in name_list] wdata.append(data) sheet = excel.pe.Sheet(wdata) response = excel.make_response(pyexcel_instance=sheet, file_name=filename, file_type="xlsx") return response
#asset-export.py from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.objializers import ModelSerializer from .models import MyAsset import django_excel as excel class ITAssetSer(ModelSerializer): class Meta: model = MyAsset fields = '__all__' class ITAssetExport(APIView): """""" use_model = MyAsset queryset = use_model.objects.all() objializer_class = ITAssetSer def post(self, request, *args, **kwargs): try: obj_list = self.use_model.objects.all() return MakeExcel(obj_list=obj_list, class_name=self.use_model, filename="itasset") except: ret = { "code": 599, "message": "導出數據出錯" } return Response(ret) def get(self, request, *args, **kwargs): self.http_method_not_allowed(request, *args, **kwargs) def MakeExcel(obj_list, class_name, filename): """ 制作excel表 :param obj_list: 內容列表 :param class_name: 類名 :param filename: 文件名 :return: """ meta_fields = class_name._meta.fields name_list = [field.name for field in meta_fields] header_list = [field.verbose_name for field in meta_fields] wdata = [header_list] for obj in obj_list: data = [] for name in name_list: value = eval("obj." + name) # 等價于getattr(obj,name),即獲取該對象某個字段內容 if isinstance(value, Model): value = eval("".join(["obj." + name + "_id"])) # 等價于obj.name_id,即獲取該對象中外鍵對象數值 data.append(value) # 參考例子 # data = [getattr(obj, name) if name != "ittype" else obj.ittype_id for name in name_list] wdata.append(data) sheet = excel.pe.Sheet(wdata) response = excel.make_response(pyexcel_instance=sheet, file_name=filename, file_type="xlsx") return response
感謝各位的閱讀,以上就是“Python django導出excel的方法是什么”的內容了,經過本文的學習后,相信大家對Python django導出excel的方法是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。