Python Django Bulk Import non-repeating data

Source: Internet
Author: User
Tags import database
This article for you to share the Python Django batch import non-repeating data implementation code for your reference, the specific content is as follows

The procedure is as follows:

#coding: Utf-8 import os os.environ.setdefault ("Django_settings_module", "www.settings") ' DJANGO When the version is greater than or equal to 1.7, you need to add the following two clauses import djangodjango.setup () otherwise it will throw an error django.core.exceptions.AppRegistryNotReady:Models aren ' t Loaded yet. " Import Djangoimport Datetimeif Django. VERSION >= (1, 7): #自动判断版本 Django.setup () from keywork.models import devdata f = open (' cs.csv ') worklist = []next (f) #将文件标 Remember to move to the next line x = y = 0for lines in f:parts = Line.replace (' "', ') #将字典中的" replace empty parts = Parts.split (', ') #按; Slice the string if Devdata.obje Cts.filter (serv_id = parts[0],day_id=parts[24]). Exists (): x = x + 1 else:y = y + 1 worklist.append (Devdata (serv_id=par Ts[0], serv_state_name=parts[1], acc_nbr=parts[2], user_name=parts[3], acct_code=parts[4], product_id=parts[5], mkt_ CHNL_NAME=PARTS[6], mkt_chnl_id=parts[7],mkt_region_name=parts[8], mkt_region_id=parts[9],mkt_grid_name=parts[10 ], sale_man=parts[11],sale_outlets_cd1_name=parts[12], completed_time=parts[13],remove_data=parts[14], user_flag=       PARTS[15],PRO_FLAG=PARTS[16], service_offer_id=parts[17],service_offer_name=parts[18], finish_time=parts[19],staff_name= PARTS[20], staff_code=parts[21],org_name=parts[22],prod_offer_name=parts[23],day_id=parts[24]) f.close () DevD Ata.objects.bulk_create (worklist) print "Duplicate data" +STR (x) + "bar, import data successfully" +str (y) + "bar"

This paper mainly uses the model exists () to determine whether the import data and database is duplicated, if not repeat, use bulk_create batch Import database!

A reference to my other article: Bulk Import Data

The above is the whole content of this article, I hope that everyone's study has helped.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.