<强> 强>
我在数据库中建了一张表,用来保存ucloud云上的项目id和项目名称
<强> 强>代码如下
#编码:utf - 8 得到django.db import 模型 class 项目(models.Model): ,,,name =, models.CharField (u的项目名称”,max_length=32,空白=True) ,,,id =, models.CharField (u的项目ID”, max_length=32,独特的=True, primary_key=True,空白=True) ,,,create_date =, models.DateTimeField (u”创建时间”,,auto_now_add=True) ,,,update_date =, models.DateTimeField (u”更新时间”,,auto_now=True) ,,,def __unicode__(自我): ,,,,,,,return self.name
<强> 强>代码如下
得到django.contrib import 管理 得到ucloud.models import * class ProjectAdmin (admin.ModelAdmin): ,,,list_display =,(“名字”,“id”) admin.site.register(项目,ProjectAdmin)
<强> 强>
现在,我准备了一个脚本,通过UcloudAPI获取到了线上所有的项目的项目ID和项目名称
# !/usr/bin/env python # - *编码:utf8 - * - 得到Ucloud_API.config import * 得到Ucloud_API.sdk import  UcloudApiClient 得到models import * def get_project_info (): ,,,ApiClient =, UcloudApiClient (base_url, public_key,, private_key) ,,,Parameters =, { ,,,,,,,“行动”:,“GetProjectList” ,,,} ,,,response =, ApiClient.get(“/?参数) ,,,ids =, ( ,,,,,,,{“ProjectId”: _ (“ProjectId”),“ProjectName”:, _ [' ProjectName ']} ,,,,,,,for _ 拷贝的反应(“ProjectSet”) ,,,,,,,) ,,,return id
# #脚本执行结果如下 [{“ProjectId”: 'org u - 81 ', ' ProjectName”: u ' \ u4e0a \ u6d77 \ u522b \ u6837 \ u7ea2 \ u4fe1 \ u606f \ u6280 \ u672f \ u6709 \ u9650 \ u516c \ u53f8 '}, {“ProjectId”:, u 'org-ja1wvv ', ' ProjectName”: u ' \ u5907 \ u6848 \ u4e13 \ u7528 '}, {“ProjectId”:, u 'org-wrg10n ', ' ProjectName”: u 'gitlab '},, {“ProjectId”:, u 'org-pni2a2 ', ' ProjectName”: u 'PublicTest '},, {“ProjectId”:, u 'org-kbxrx4 ', ' ProjectName”: u 'SPMS '},, {“ProjectId”:, u 'org-aws3dj ', ' ProjectName”: u ' \ u5b89 \ u5168 \ u6d4b \ u8bd5 '}, {“ProjectId”:, u 'org-vzfixt ', ' ProjectName”: u 'OTA '},, {“ProjectId”:, u 'org-et55qg ', ' ProjectName”: u ' 99 \ u6570 \ u636e \ u540c \ u6b65 \ u4e2d \ u8f6c \ uff0c \ u672c \ u9879 \ u76ee \ u4e0e99 \ u5185 \ u7f51 \ u6253 \ u901a \ uff0c \ u4e0d \ u5141 \ u8bb8 \ u6dfb \ u52a0 \ u4efb \ u4f55 \ u673a \ u5668 '}, {“ProjectId”:, u 'org-ghan2t ', ' ProjectName”: u 'Ops '},, {“ProjectId”:, u 'org-qf4d2n ', ' ProjectName”: u 'iPms '})
ProjectId对象表中字的id段,ProjectName对应表中名字的字段。
下面是批量导入数据的代码:
# !/usr/bin/env python # - *编码:utf8 - * - 得到Ucloud_API.config import * 得到Ucloud_API.sdk import  UcloudApiClient 得到models import * # #通过API获取数据 def get_project_info (): ,,,ApiClient =, UcloudApiClient (base_url, public_key,, private_key) ,,,Parameters =, { ,,,,,,,“行动”:,“GetProjectList” ,,,} ,,,response =, ApiClient.get(“/?参数) ,,,ids =, ( ,,,,,,,{“ProjectId”: _ (“ProjectId”),“ProjectName”:, _ [' ProjectName ']} ,,,,,,,for _ 拷贝的反应(“ProjectSet”) ,,,,,,,) ,,,return id # #批量导入数据 def update_project_info (): ,,,info =, get_project_info () ,,,pids =, [] ,,,for _ 拷贝信息: ,,,,,,,projectid =, _ (“ProjectId”) ,,,,,,,pids.append (projectid) ,,,,,,,project =,没有 ,,,,,,,试一试: ,,,,,,,,,,,project =, Project.objects.get (pk=projectid) ,,,,,,,except Project.DoesNotExist: null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nulldjango批量导入数据