我将数据从对象保存到数据库时遇到问题。
这是我的 view.py :
<div class="boxWithBorder">
<div class="header">
<h2 class="header__element"></h2>
</div>
</div>
models.py :
#-*- coding: utf-8 -*-
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from testApp.models import testDb
@csrf_exempt
def doSomething(request):
p = testDb.objects.all()
p.delete()
names = [] if 'names' not in request.POST else json.loads( request.POST[ 'names' ] )
ids = [] if 'ids' not in request.POST else json.loads( request.POST[ 'ids' ] )
for i in range(0, len(names) ):
p = testDb(
name=str( names[ i ] ),
id=int( ids[ i ] )
)
p.save()
return HttpResponse("Done")
每次我看到同样的错误:
TypeError:'QuerySet'对象在此行中不可调用:
id = int(ids [i])
你知道是什么原因吗?
答案 0 :(得分:-2)
也许尝试这样的事情,
@csrf_exempt
def doSomething(request):
names = [] if 'names' not in request.POST else json.loads( request.POST[ 'names' ] )
ids = [] if 'ids' not in request.POST else json.loads( request.POST[ 'ids' ] )
for index, item in enumerate(names):
testdb.objects.create(name=item['name'], id=ids[index]['id'])
return HttpResponse("Done")