def post(self, *args, **kwargs): # 第一种方式 stu = Students() stu.s_name = u'小四' session.add(stu) session.commit() # 第二种方式: 批量添加 stu_list = [] for i in range(5): stu = Students() stu.s_name = '小明_%s' % i stu_list.append(stu) session.add_all(stu_list) session.commit() self.write('创建学生信息成功')
def student_register(): if request.method == 'GET': return render_template('StudentRegister.html') elif request.method == 'POST': username = request.form.get('username') password = request.form.get('password') students = Students() students.s_name = username students.password = password db.session.add(students) db.session.commit() return 'Register Success'
def create_stu_all(): # 批量创建,add_all() names = ['小王', '老王', '厂长', '莉哥', '温婉'] stu_list = [] for name in names: stu = Students() stu.s_name = name # stu.save() stu_list.append(stu) # db.session.add(stu) db.session.add_all(stu_list) db.session.commit() return '批量创建成功'
def create_stu(): # 实现创建, add() stu = Students() stu.s_name = '小明1' stu.save() return '创建学生信息成功'