django批量导入数据
一、需求
网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了合肥免费建站欢迎大家使用!
我在数据库中建了一张表,用来保存ucloud云上的project id 和project name
models.py代码如下
#coding:utf-8 from django.db import models class Project(models.Model): name = models.CharField(u'项目名称',max_length=32,blank=True) id = models.CharField(u'项目ID',max_length=32,unique=True,primary_key=True,blank=True) create_date = models.DateTimeField(u'创建时间', auto_now_add=True) update_date = models.DateTimeField(u'更新时间', auto_now=True) def __unicode__(self): return self.name
admin.py代码如下
from django.contrib import admin from ucloud.models import * class ProjectAdmin(admin.ModelAdmin): list_display = ['name','id'] admin.site.register(Project,ProjectAdmin)
二、批量导入脚本
现在,我准备了一个脚本,通过UcloudAPI获取到了线上所有的项目的项目ID和项目名称
#!/usr/bin/env python #-*-coding:utf8-*- from Ucloud_API.config import * from Ucloud_API.sdk import UcloudApiClient from models import * def get_project_info(): ApiClient = UcloudApiClient(base_url, public_key, private_key) Parameters = { "Action": "GetProjectList" } response = ApiClient.get("/", Parameters) ids = [ {'ProjectId': _['ProjectId'], 'ProjectName': _['ProjectName']} for _ in response['ProjectSet'] ] return ids
##脚本执行结果如下 [{'ProjectId': u'org-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对应表中的name字段。
下面是批量导入数据的代码:
#!/usr/bin/env python #-*-coding:utf8-*- from Ucloud_API.config import * from Ucloud_API.sdk import UcloudApiClient from models import * ##通过API获取数据 def get_project_info(): ApiClient = UcloudApiClient(base_url, public_key, private_key) Parameters = { "Action": "GetProjectList" } response = ApiClient.get("/", Parameters) ids = [ {'ProjectId': _['ProjectId'], 'ProjectName': _['ProjectName']} for _ in response['ProjectSet'] ] return ids ##批量导入数据 def update_project_info(): info = get_project_info() pids = [] for _ in info: projectid = _['ProjectId'] pids.append(projectid) project = None try: project = Project.objects.get(pk=projectid) except Project.DoesNotExist: project = Project(pk=projectid) project.name = _['ProjectName'] project.save() # 删除本地有但是ucloud上没有的项目 projectids = [_['pk'] for _ in Project.objects.all().values('pk')] diff_ids = list(set(projectids).difference(set(pids))) Project.objects.filter(pk__in=diff_ids).delete()
这里导入数据的思路如下:
try: project = Project.objects.get(pk=projectid) except Project.DoesNotExist: project = Project(pk=projectid)
先通过get方法获取对象,如果对象bu'存在,则用下面的方法创建该对象
project = Project(pk=projectid)
如果对象存在,通过下面的方法更新ProjectName
project.name = _['ProjectName'] project.save()
另外还有一个需要注意的地方是,数据库中的数据要更新。
例如:线上之前有一个项目aaa,但是后来被删除了,那么则需要从数据库中删除该对象。
使用以下方法;
projectids = [_['pk'] for _ in Project.objects.all().values('pk')] diff_ids = list(set(projectids).difference(set(pids))) Project.objects.filter(pk__in=diff_ids).delete()
将数据库中的对象列表转换成集合,将线上获取到的对象列表也转换成集合,然后通过
set1.difference(set2)
的方法来获取在set1中存在,但在set2中不存在的对象,然后用下面的方法删除
Project.objects.filter(pk__in=diff_ids).delete()
批量导入数据还可以使用下面的方法执行
def update_project_info(): info = get_project_info() pids = [_['ProjectId'] for _ in info] for i in info: Project.objects.get_or_create(id=i['ProjectId'],name=i['ProjectName']) projectids = [_['pk'] for _ in Project.objects.all().values('pk')] #获取数据库中存在,但线上不存在的projectid diff_ids = list(set(projectids).difference(set(pids))) #删除数据库中多余的数据 Project.objects.filter(pk__in=diff_ids).delete()
Project.objects.get_or_create(id=i['ProjectId'],name=i['ProjectName'])
get_or_create() 有就获取过来,没有就创建,用它可以避免重复,但是速度可以会慢些,因为要先尝试获取,看看有没有
三、如何执行脚本
我们可以将导入数据的方法通过问url的方式,来执行
#coding:utf-8 from django.shortcuts import render from django.http import HttpResponse,JsonResponse from sdkucloud import * # Create your views here. def index(request): return HttpResponse('index') def pull_project(request): update_project_info() return HttpResponse('OK!')
编辑$APPName/urls.py
from django.conf.urls import include, url from django.contrib import admin from ucloud.views import * urlpatterns = [ url(r'^$', index), url(r'^pull_project/$', pull_project) ]
网页名称:django批量导入数据
网站网址:http://ybzwz.com/article/gjhsid.html