我无法弄清如何通过id获取 Django 模型中的用户,即 django.contrib.auth.models.User。我想要删除一个用户,所以我正在尝试这样查找:
User.objects.get(id=request.POST['id'])
但它不起作用,并返回
User matching query does not exist.
id是通过ajax发送的:
$("#dynamic-table").on('click','.member_delete_btn', function() {
if (confirm("Are you sure? the member will be deleted...") == true) {
$.ajax({
type: "POST",
url: "/panel/member/delete/",
data: { id: $(this).attr('data-id'), 'csrfmiddlewaretoken': '{{ csrf_token }}' },
success: function (data) {
if(data.success) {
$('#result').html('<div class="alert alert-success"> <strong>Well done!</strong> Member deleted.</div>');
list_members();
}else{
$('#result').html('<div class="alert alert-warning"> <strong>Warning!</strong> Member not deleted.</div>');
}
},
error: function (data) {
alert("failure:" + data.error);
}
});
}
else {
return false;
}
return false;
});
我调试了它,一切正常,用户存在于数据库中,ID也正确。
我该怎么做呢? Django User 实例是否有任何删除方法?
谢谢