The code given below is the program to import data into my django model, actually what this program does is it read the file and it import each line from the file to the django model, here may be 1000 files will be there,there will be some error in some file and the program will catch the exception if the file have any type of error and log it, But what i need is to rollback the entire transaction from one file which throws an error.I want to do this in postgres
def impSecOrdr_File(self,path):
lines=1
try:
fromFile=open(path)
for eachLine in fromFile:
obj = SecOrdr_File()
if lines!=1:
fieldsInline = eachLine.split(",")
obj.dates = dates
obj.column1 = fieldsInline[0].strip()
obj.column2 = fieldsInline[1].strip()
obj.column3 = float(fieldsInline[2].strip())
obj.column4 = float(fieldsInline[3].strip())
obj.save()
lines+=1
except BaseException as e:
transaction.rollback()
logging.info('\tError in importing %s line %d : %s' % (path, lines, e.__str__()))
else:
try:
logging.info("\tImported %s, %d lines" % (path, lines))
except Exception as e:
logging.info(e)
Is there any way to do that. I went through the django transaction document and I tried Some of these but it is not working.. Can any one help me please