django 多对多关系表批量导入excel数据表

it2022-05-07  0

# coding:utf-8 import sys from django.core.management.base import BaseCommand, CommandError from django.core.management import call_command import json,pdb,hashlib from demo.models import * import xlrd class Command(BaseCommand): """ 从文件中读取QA语料,并存储语义QA和普通QA中 """ # option_list = BaseCommand.option_list + ( # make_option('--file_path',help="file_path",type="string"), # make_option('--service_sha1',help="service_sha1",type="string"), # ) def add_arguments(self, parser): parser.add_argument( '-fp', '--file_path', action='store', dest='file_path', default='close', help='lala', ) parser.add_argument( '-ss', '--service_sha1', action='store', dest='service_sha1', default='close', help='kaka', ) def handle(self, *args, **options): # 获取参数 file_path = options['file_path'] service_sha1 = options['service_sha1'] # 查找应用 try: service = Service.objects.get(sha1=service_sha1) except Exception as e: print('用户不存在') # 读取文件 data = xlrd.open_workbook(file_path) print('------') # 分别处理每个表单 sheets = data.sheets() for sheet in sheets: nrows = sheet.nrows # 遍历每一行数据 for i in range(nrows): if i == 0: continue row_list = sheet.row_values(i) demo_sha1 = row_list[0].strip() demo_name = row_list[1].strip() demo_content = row_list[2].strip() # 检查语料是否存在 is_exist = TMLDemo.objects.filter(sha1=demo_sha1).exists() if not is_exist: # 语料存储到数据库 tmldemo = TMLDemo( sha1 = demo_sha1, demo_name = demo_name, demo_content = demo_content, ) tmldemo.save() # 计算sha1 item_sha1 = hashlib.sha1() item_sha1.update((service_sha1 + demo_sha1).encode()) item_sha1 = item_sha1.hexdigest() is_item_exist = DemoServiceRelation.objects.filter(sha1=item_sha1).exists() if not is_item_exist: item = DemoServiceRelation( sha1 = item_sha1, demo_sha1 = demo_sha1, service_sha1 = service_sha1, ) item.save()

最新回复(0)