PythonDjango batch import of non-duplicate data

Source: Internet
Author: User
This article mainly introduces materials related to batch import of non-duplicate data in pythonDjango. You can refer to this article to share the implementation code of batch import of non-duplicate data in python Django, for your reference, the specific content is as follows:

The procedure is as follows:

# Coding: UTF-8 import OS. environ. setdefault ("DJANGO_SETTINGS_MODULE", "www. settings ") ''' when the Django version is greater than or equal to 1.7, add the following two statements: import djangodjango. setup () otherwise, the error django will be thrown. core. exceptions. appRegistryNotReady: Models aren't loaded yet. '''import djangoimport datetimeif django. VERSION> = (1, 7): # automatically determines the VERSION of django. setup () from keywork. models import DevData f = open('cs.csv ') WorkList = [] next (f) # Move the file tag to the next line x = y = 0for line in f: parts = line. replace ('"','') # replace "in the dictionary with null parts = parts. split (',') # Press; to segment the string if DevData. objects. filter (serv_id = parts [0], day_id = parts [24]). exists (): x = x + 1 else: y = y + 1 WorkList. append (DevData (serv_id = parts [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 () DevData. objects. bulk_create (WorkList) print "duplicate data" + str (x) + "entries, imported data" + str (y) + "entries"

This article mainly uses the exists () in the model to determine whether the imported data and database are repeated. If there are no duplicates, use bulk_create to import the database in batches!

For more information, see my other article:Batch import data

The above is all the content of this article, hoping to help you learn.

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: info-contact@alibabacloud.com 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.