djangoを単独で使用するormトランザクション

3855 ワード

import os
import datetime
from django.conf import settings
os.environ['DJANGO_SETTINGS_MODULE'] = "settings" # transaction settings ,transaction settings
from django.db import transaction

if not settings.configured:
settings.configure(
#DATABASE_ENGINE = 'sqlite3',
#DATABASE_NAME= os.path.join( os.path.dirname(__file__), 'db\\sqlite3.db'),
DATABASES = {
'default': {
'ENGINE': 'sqlite3',
'NAME': os.path.join( os.path.dirname(__file__), '\\sqlite3.db'),
}
}
)


from django.db import models
class Person(models.Model):
class Meta:
db_table = 'persons'
app_label = 'test'

created_at = models.DateTimeField('created_at', auto_now_add = True)
updated_at = models.DateTimeField('updated_at', auto_now = True)
name = models.CharField("name", max_length = 20)
age = models.IntegerField("age")
time1 = datetime.datetime.now()
with transaction.commit_on_success():
for i in range(100000):
p = Person()
p.name = "a"
p.age = 111
p.save()
time2 = datetime.datetime.now()
print (time2-time1).seconds