def test_cache(self): fname = 'Mehroze' lname = 'Zahid' email = '*****@*****.**' Person.objects.create(email=email, first_name=fname, last_name=lname) with self.assertNumQueries(1): Person.get_person(email) Person.get_person(email)
def create_user(): #创建数据 # u = Person( # name = "张三" # ) # #保存到数据库 # db.session.add(u) #批量创建 persons = [] for i in range(10): u = Person(name="张三" + str(i)) persons.append(u) db.session.add_all(persons) db.session.commit() return "创建完毕"
def get_name(reqest): if ('firstName' in reqest.session): return HttpResponseRedirect('/thanks') if (reqest.method == 'POST'): form = NameForm(reqest.POST, reqest.FILES) if (form.is_valid()): print(form.cleaned_data) reqest.session['firstName'] = form.cleaned_data['firstName'] person = Person.create(form.cleaned_data['firstName'], form.cleaned_data['lastName'], form.cleaned_data['picture'], form.cleaned_data['profileVideo']) person.save() print(form.cleaned_data) return HttpResponseRedirect('/thanks') else: form = NameForm() return render(reqest, 'name.html', {'form': form})
def upload_file(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): file = request.FILES['file'] csv_data = pd.read_csv(file) personObjs = [ Person(name=row['Name'], age=row['Age'], designation=row['Designation']) for index, row in csv_data.iterrows() ] Person.objects.bulk_create(personObjs) return HttpResponse("<h1>Successfully data inserted</h1>") else: form = UploadFileForm() return render(request, 'index.html', {'form': form})
def show_person(request, key): return object_detail(request, Person.all(), key)
def list_people(request): return object_list(request, Person.all(), paginate_by=10)