zoukankan      html  css  js  c++  java
  • django django-import-export 自定义字段的实现

    目前有两种比较推荐的方法:

    1. 重写export方法在export方法中将表头及数据造出来,只需要将数据写入列表中即可,推荐使用sql,连表查询比较灵活,需要注意的是表头要和数据一一对应,不然报错:
    class IncomingValuableGroupViewResource(resources.ModelResource):
        class Meta:
            model = IncomingValuableGroupView
            # fields = ('id', 'author_name',)
    
        def export(self, queryset=None, *args, **kwargs):
            headers = ['序号', '日期', '清点SKU种类', '清点SKU数量', '质检SKU种类', '质检SKU数量', '加工SKU种类', '加工SKU数量', '上架SKU种类', '上架SKU数量',
                       '目的仓库']
            data = tablib.Dataset(headers=headers)
            from django.db import connection
            cr = connection.cursor()
            sql = """
                    select A.id,
                           A.create_date_without_time,
                           A.count_product_check,
                           A.sum_actual_check_qty,
                           A.count_product_quality,
                           A.sum_actual_quality_qty,
                           A.count_product_processing,
                           A.sum_actual_processing_qty,
                           A.count_product_shelves,
                           A.sum_actual_shelves_qty,
                           B.name as location_name
                    from incoming_valuable_group_view as A,
                         stock_location as B
                    where A.location_id = B.id;
            """
            cr.execute(sql)
            res = cr.fetchall()
    
            for insert_data in res:
                data.append(insert_data)
    
            self.after_export(queryset, data, *args, **kwargs)
    
            return data
    
    1. 我目前没有使用过,但也挺简单的,不需要重写export方法,直接在数据库中创建视图,并在项目中创建相应的模型,视图,按照常规的文件下载方式设置即可,这里比较麻烦的是需要重新创建模型,但是以后维护方便,以后加减字段,调整逻辑比较方便.
  • 相关阅读:
    Android数据库升级,数据不丢失解决方案
    Android项目中单实例数据库类,解决database is locked
    Android彩蛋效果,微信彩蛋效果
    Android性能优化
    Unable to execute dex: method ID not in [0, 0xffff]: 65536
    Android下载速度计算
    Android中不混淆类中函数
    Android中Parcelable接口用法
    开启Ubuntu Linux下VirtualBox访问USB功能
    touch移动触屏滑动事件
  • 原文地址:https://www.cnblogs.com/qianxunman/p/13724316.html
Copyright © 2011-2022 走看看