我有一个称为“编辑”的视图,该视图可编辑对象。我希望用户只能在未锁定的情况下对其进行编辑,这就是说,一个属性称为locked = BooleanField(),并在视图中首先检查对象是否被锁定,然后再继续操作。
到目前为止,这是编辑功能:
@login_required
def editdossier(request, pk):
dossier = get_object_or_404(Dossier, id=pk)
form = AddDossierForm(request.POST or None, instance = dossier)
context = {'form': form}
if form.is_valid():
dossier = form.save(commit= False)
dossier.save()
context = {
'form': form,
'dossiers': Dossier.objects.all()
}
return render(request, 'dashboard/home.html', context)
else:
context= {'form': form}
return render(request,'dashboard/modifier_dossier.html' , context)
这就是我想要做的:
@login_required
def editdossier(request, pk):
dossier = get_object_or_404(Dossier, id=pk)
# CHECK IF DOSSIER.LOCKED == FALSE:
form = AddDossierForm(request.POST or None, instance = dossier)
context = {'form': form}
if form.is_valid():
dossier = form.save(commit= False)
dossier.save()
context = {
'form': form,
'dossiers': Dossier.objects.all()
}
return render(request, 'dashboard/home.html', context)
else:
context= {'form': form}
return render(request,'dashboard/modifier_dossier.html' , context)
我做了以下检查:
if dossier.locked == false:
proceed
,但不检查条件。我尝试了if dossier[0].locked == false
,但显示错误消息,表明该对象不可索引。