/ students / delete_gr / 234 /
处的FieldError
我正在尝试删除gr
。它不起作用,出现错误:
无法将关键字
id
解析为字段。选项包括:地址,area_code,area_code_id,class_register,classes_A,classes_A_id,classes_C,classes_C_id,date_birth,feel,first_name,gardian,性别,gr_no,last_name,section_A,section_A_id,section_C,section_C_id,状态
views.py
def delete_gr(request, pk):
gr_register.objects.filter(id=pk).delete()
gr = gr_register.objects.all()
context = {
'gr' : gr
}
return render(request, 'students/home.html', context)
viewdata.html
{% extends 'authenticate/base.html' %}
{% block content %}
<div class="container">
<h2 class="text-center" style="font-family:sans-serif;">GR-Record</h2>
<hr/>
<div class="addgr text-right" style="margin-bottom: -50px;">
<button class="btn btn-dark" type="submit"><a href="{% url 'addgrregister' %}">Add-GR Register</a></button>
</div>
<form class="form-inline my-2 my-lg-2">
<input
class="form-control mr-sm-2"
type="search"
placeholder="Search"
aria-label="Search"
name="search"
value="{{search_term}}">
<button class="btn btn-outline-dark my-2 my-sm-0" type="submit">Search</button>
</form>
<br/>
<div class="table-responsive ">
<table class="table table-hover table-dark table-bordered table-sm">
<caption>List of GR-students</caption>
<thead>
<tr>
<th>GR No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Date Of Birth</th>
<th>Class Of Admission</th>
<th>Section Of Admission</th>
<th>Gender</th>
<th>Current Class</th>
<th>Current Section</th>
<th>Address</th>
<th>Area Code</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for gr in gr %}
<tr>
<td>{{gr.gr_no}}</td>
<td>{{gr.first_name}}</td>
<td>{{gr.last_name}}</td>
<td>{{gr.date_birth}}</td>
<td>{{gr.classes_A}}</td>
<td>{{gr.sections_A}}</td>
<td>{{gr.gender}}</td>
<td>{{gr.classes_C}}</td>
<td>{{gr.sections_C}}</td>
<td>{{gr.address}}</td>
<td>{{gr.area_code}}</td>
<td>{{gr.status}}</td>
<td>
<a href="{% url 'editgr' gr.pk %}" class="btn btn-warning btn-sm" role="button"> Edit</a><br/>
<a href="{% url 'delete_gr' gr.pk %}" class="btn btn-danger btn-sm" role="button"> Delete</a>
</td>
</tr>
{%endfor%}
</tbody>
</table>
</div>
<br/><br/>
</div>
{% endblock %}
答案 0 :(得分:0)
您收到此错误,因为在id
模型中没有名为gr_register
的字段。
无法将关键字“ id”解析为字段。
如果没有为模型明确定义主键,那么将仅自动为模型创建id
。
使用此查询来获取gr_register查询集。
gr_register.objects.filter(pk=pk).delete()