我试图通过Index删除QuerySet中的Item。我可以使用print q_set[code_id - 1]
显示QuerySet的项目,但无法使用del q_set[code_id - 1]
删除它。我想永久删除该项目,而不是过滤掉该项目。
我收到此错误:
TypeError at /lessons/customcode/5/delete
'QuerySet' object doesn't support item deletion
views.py
...
def customcode_view(request):
global q_set
try:
u = User.objects.get(username=request.user)
except:
return render(request,"login_required.html",{})
q_set = customcode.objects.filter(user=u)
if request.method == "POST":
form = CustomcodeForm(request.POST)
if form.is_valid():
cc = form.save(commit=False)
cc.user = u
cc.save()
return HttpResponseRedirect('#BOTTOM')
else:
form = CustomcodeForm()
return render(request, "customcode.html" , {'q_set':q_set,'form':form,})
def deletecode(request,code_id):
code_id = int(code_id)
del q_set[code_id - 1] #this is the problem
return redirect('customcode_view')
...
models.py
...
class customcode(models.Model):
user = models.ForeignKey(User)
name = models.CharField(blank=False,max_length=250)
sourcecode = models.TextField(blank=False)
def __unicode__(self):
return self.name
...
答案 0 :(得分:3)
答案 1 :(得分:0)
如果您只想删除列表中的条目而不是数据库中的行,请尝试以下操作:
'products' => isset($products) ? $products : '',