您好,登錄后才能下訂單哦!
一、需求
我在數據庫中建了一張表,用來保存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) ]
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。