How can I save the whole DB transaction if there i

2019-06-02 15:56发布

问题:

I have many files in a folder I am open the folder and import all the data inside all the files into my model, Here My program import data's in one file at a time,so it will read all data in the file once and I iterate through each line at the end of each line I save the transaction to database using obj.save().

Here I am facing some exception while import may type mismatch error so the program stop import of that file and half of the data will be imported and it move to next file. So this problem cause duplicate data so here I need save the transaction using obj.save() of a file if there is no exception how can i achieve this can any one 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:

You can use a transaction.

So your function should become 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 commit all work done within the function at that point. If the function raises an exception, though, Django will roll back the transaction.