def employee_delete(request, id=None):
user = get_object_or_404(User, id=id)
#user = User.objects.get(id=id)
if request.method == 'POST':
print(type(user))
self.user.delete()
return HttpResponseRedirect(reverse('employee_list'))
else:
context = {}
context['user'] = user
return render(request, 'employee/delete.html', context)
在django 2.1.5中使用delete()删除对象时显示错误
答案 0 :(得分:1)
您正在创建基于功能的视图。因此请勿使用self
def employee_delete(request, id=None):
user = get_object_or_404(User, id=id)
#user = User.objects.get(id=id)
if request.method == 'POST':
print(type(user))
user.delete() # change here
return HttpResponseRedirect(reverse('employee_list'))
else:
context = {}
context['user'] = user
return render(request, 'employee/delete.html', context)
更新
在您的models.py
中,您使用字符串CASCADE
user = models.OneToOneField(User, on_delete=models.CASCADE) # update like this