Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Django batch Import data

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

I. demand

I built a table in the database to hold project id and project name on the ucloud cloud

The models.py code is as follows

# coding:utf-8from django.db import modelsclass Project (models.Model): name = models.CharField (u 'project name', max_length=32,blank=True) id = models.CharField (u 'project ID',max_length=32,unique=True,primary_key=True,blank=True) create_date = models.DateTimeField (u' creation time', auto_now_add=True) update_date = models.DateTimeField (u 'update time' Auto_now=True) def _ _ unicode__ (self): return self.name

The admin.py code is as follows

From django.contrib import adminfrom ucloud.models import * class ProjectAdmin (admin.ModelAdmin): list_display = ['name','id'] admin.site.register (Project,ProjectAdmin)

Second, bulk import scripts

Now, I have prepared a script to get the project ID and project names of all the projects online through UcloudAPI

#! / usr/bin/env python#-*-coding:utf8-*-from Ucloud_API.config import * from Ucloud_API.sdk import UcloudApiClientfrom models import * def get_project_info (): ApiClient = UcloudApiClient (base_url, public_key, private_key) Parameters = {"Action": "GetProjectList"} response = ApiClient.get ("/", Parameters) ids = [{'ProjectId': _ [' ProjectId'] The execution result of the 'ProjectName': _ [' ProjectName']} for _ in response ['ProjectSet']] return ids## script is as follows: [{' ProjectId': ujuorglu 81, 'ProjectName': u'\ u4e0a\ u6d77\ u522b\ u6837\ u7ea2\ u4fe1\ u6709\ u9650\ u516c\ u53f8'}, {'ProjectId': uuuorgja1wvcentury,' ProjectName': u'\ u5907\ u6848\ u4e13\ u75288'} {'ProjectId': uplift u6d4b'}, {'ProjectId': uplift u6d4b'}, {'ProjectId': upright org u6d4b pni2a2,' ProjectName': upright PublicTest'}, {'ProjectId': upright orgMuffill kbxrx4bath,' ProjectName': upright SPMS'}, {'ProjectId': uplift aws3djacks,' ProjectName': u'\ u5b89\ u5168\ u5168\ u8bd5'}, {'ProjectId': uaccounorglyvzfixthouses,' ProjectName': ubug OTA'}} {'ProjectId': umakers 99\ u6570\ u636e\ u540c\ u6b65\ u4e2d\ u8f6c\ uff0c\ u672c\ u9879\ u76ee\ u4e0e99\ u6253\ u901a\ uff0c\ u4e0d\ u5141\ u8bb8\ u6dfb\ u52a0\ u4f55\ u673a\ u5668'}, {' ProjectId': ufarmers orglyghan2ts, 'ProjectName': uroads Ops`}, {' ProjectId': umakers orglyqf4d2nails, 'ProjectName': umakers iPms`}]

The id field in the ProjectId object table and the name field in the ProjectName corresponding table.

The following is the code for bulk importing data:

#! / usr/bin/env python#-*-coding:utf8-*-from Ucloud_API.config import * from Ucloud_API.sdk import UcloudApiClientfrom models import * # obtain data through 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## bulk import data 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 () # Delete items that are locally available but not on 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 ()

The idea of importing data here is as follows:

Try: project = Project.objects.get (pk=projectid) except Project.DoesNotExist: project = Project (pk=projectid)

First get the object through the get method, and if the object bu' exists, create the object with the following method

Project = Project (pk=projectid)

If the object exists, update ProjectName by using the following method

Project.name = _ ['ProjectName'] project.save ()

Another thing to note is that the data in the database needs to be updated.

For example, if there was a project aaa online, but it was later deleted, then you need to delete the object from the database.

Use the following methods

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 ()

Convert the list of objects in the database into a collection, and also convert the list of objects obtained online into a collection, and then use the

Set1.difference (set2)

To get objects that exist in set1 but not in set2, and then delete them with the following method

Project.objects.filter (pk__in=diff_ids) .delete ()

Bulk importing data can also be performed using the following methods

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')] # get the existence in the database But projectid diff_ids = list (set (projectids). Difference (set (pids)) # deletes redundant data in the database Project.objects.filter (pk__in=diff_ids). Delete () Project.objects.get_or_create (id=i ['ProjectId'], name=i [' ProjectName'])

Get_or_create () can be obtained as soon as it is available, and created without it. Using it can avoid repetition, but the speed can be slower, because you have to try to get it first to see if there is any.

How to execute the script

We can execute the method of importing data by asking url.

# coding:utf-8from django.shortcuts import renderfrom django.http import HttpResponse,JsonResponsefrom sdkucloud import * # Create your views here.def index (request): return HttpResponse ('index') def pull_project (request): update_project_info () return HttpResponse (' OKTV')

Edit $APPName/urls.py

From django.conf.urls import include, urlfrom django.contrib import adminfrom ucloud.views import * urlpatterns = [url (r'^ $', index), url (r'^ pull_project/$', pull_project)]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report