Since I saved nearly 30 daily tasks from TODO Plan, I had to access the database hundreds of times by creating regular tasks, and I wanted to reduce instability and load on the database.
Even so, I don't understand what's wrong with the database because it's accessed a lot ... To be clear, I don't know anything other than speed (laughs).
This implementation is
It is an implementation method called bulk_create. Referenced page: https://tsukasa-blog.com/programming/bulk-create-update/
As a result, the access to the database was reduced to 2 times (⌒∇⌒)
todo/views.py
def todoplancreatefunc(request):
if request.method == 'GET':
return render(request,'todo/todoplancreate.html')
else:
yearmonth = request.POST['yearmonth']
year = int(yearmonth[0:4])
month= int(yearmonth[5:7])
lastday = calendar.monthrange(year, month)[1]
new_object_list =[]
#Process the set amount every day
todo_plan = Todo_PlanModel.objects.order_by("hyoujijyun")
for todoplan in todo_plan:
if todoplan.cycle == "daily":
#Process the set amount every day
for i in range(lastday):
sakuseiday = datetime.date(year,month,i + 1)
new_object = TodoModel(
title = todoplan.title,
memo = todoplan.memo,
priority = todoplan.priority,
shisetsu_name = todoplan.shisetsu_name,
user = todoplan.user,
plants_startdate = sakuseiday,
plants_enddate = sakuseiday,
)
new_object_list.append(new_object)
elif todoplan.cycle == "everyweek":
#Process weekly
for i in range(5):
day = get_day_of_nth_dow(year, month, i + 1, int(todoplan.youbi))
if day != None:
sakuseiday = datetime.date(year, month, day)
new_object = TodoModel(
title = todoplan.title,
memo = todoplan.memo,
priority = todoplan.priority,
shisetsu_name = todoplan.shisetsu_name,
user = todoplan.user,
plants_startdate = sakuseiday,
plants_enddate = sakuseiday,
)
new_object_list.append(new_object)
elif todoplan.cycle == "monthly":
#Process monthly
#Get the end of the target month
lastday = calendar.monthrange(year, month)[1]
#Replace if the specified date is greater than the date at the end of the month
if todoplan.day > lastday:
day = lastday
else:
day = todoplan.day
sakuseiday = datetime.date(year, month, day)
new_object = TodoModel(
title = todoplan.title,
memo = todoplan.memo,
priority = todoplan.priority,
shisetsu_name = todoplan.shisetsu_name,
user = todoplan.user,
plants_startdate = sakuseiday,
plants_enddate = sakuseiday,
)
new_object_list.append(new_object)
elif todoplan.cycle == "day":
#Process month / date specification
#Get the end of the target month
lastday = calendar.monthrange(year, int(todoplan.month))[1]
#Replace if the specified date is greater than the date at the end of the month
if todoplan.day > lastday:
day = lastday
else:
day = todoplan.day
sakuseiday = datetime.date(year, int(todoplan.month), day)
new_object = TodoModel(
title = todoplan.title,
memo = todoplan.memo,
priority = todoplan.priority,
shisetsu_name = todoplan.shisetsu_name,
user = todoplan.user,
plants_startdate = sakuseiday,
plants_enddate = sakuseiday,
)
new_object_list.append(new_object)
elif todoplan.cycle == "week":
#Designated on weekdays
day = get_day_of_nth_dow(year, month, todoplan.week, int(todoplan.youbi))
if day == None:
day = lastday = calendar.monthrange(year, month)[1]
sakuseiday = datetime.date(year, month, day)
new_object = TodoModel(
title = todoplan.title,
memo = todoplan.memo,
priority = todoplan.priority,
shisetsu_name = todoplan.shisetsu_name,
user = todoplan.user,
plants_startdate = sakuseiday,
plants_enddate = sakuseiday,
)
new_object_list.append(new_object)
TodoModel.objects.bulk_create(new_object_list)
messages = 'Processing is finished'
context = {
'messages': messages,
}
return render(request,'todo/todoplancreate.html', context)
First, it is extracted from the table, and finally, it is saved in bulk with bule_create.
There is no doubt that this has made it faster (⌒∇⌒)
Recommended Posts