How can I save the entire database transaction if there is no error in python

I have many files in a folder. I open the folder and import all the data from all the files into my model. Here My program imports data into one file at a time, so it will read all the data in the file once and I repeat every line at the end of each line. I save a transaction in a database using obj.save() .

Here, I encountered some exceptions, while import may introduce a mismatch error, so the program stops importing this file, and half of the data will be imported and move to the next file. So this problem causes duplicate data, so here I need to save the transaction using the obj.save() file, if there is no exception, how can I achieve this, can someone help me

  fromFile=open(path) for eachLine in fromFile: obj = SAMP() fieldsInline = eachLine.split(",") n=len(fieldsInline) if lines!=1: #obj.dates = dates obj.col1 = fieldsInline[0].strip() obj.col2 = fieldsInline[1].strip() obj.col3 = fieldsInline[2].strip() obj.col4 = fieldsInline[3].strip() obj.save() lines+=1 except BaseException as e: logging.info('\tError in importing %s line %d : %s' % (path, lines, e.__str__())) else: logging.info("\tImported %s, %d lines" % (path, lines)) 
+1
source share
1 answer

You can use transaction .

So your function should look something like this:

 from django.db import transaction @transaction.commit_on_success def func(): pass 

or

 from django.db import transaction def func(): # Some code with transaction.commit_on_success(): pass 

If the function returns successfully, then Django will do all the work done inside that function. If a function throws an exception, Django will reject the transaction.

0
source

Source: https://habr.com/ru/post/1314664/


All Articles