Django 如何在插入数据后立即获取id?

Django 如何在插入数据后立即获取id?,django,django-views,Django,Django Views,我希望这个标题足以理解我的问题所在 我的views.py中有此代码 enroll = StudentsEnrollmentRecord( Student_Users=student,Old_Student=old,New_Student=new, Payment_Type=payment,ESC_Student=esc,Last_School_Attended=last,Address_OF_School_Attended=add, Educati

我希望这个标题足以理解我的问题所在

我的views.py中有此代码

enroll = StudentsEnrollmentRecord(
            Student_Users=student,Old_Student=old,New_Student=new, Payment_Type=payment,ESC_Student=esc,Last_School_Attended=last,Address_OF_School_Attended=add,
            Education_Levels=educationlevel,School_Year=schoolyear,Courses=course,strands=strands,Student_Types=stype,GWA=gwa
        )
    enroll.save()

 studentenrolment = StudentsEnrollmentRecord.objects.filter(Student_Users=enroll)
 return render(request, "print.html", {"studentenrolment":studentenrolment})
这就是我得到的错误

这是我的print.html

{% for i in studentenrolment %}
    <tr>
<tr>
        <td>SURNAME</td>
        <td colspan="6"><input type="text" size="107" value="{{i.Lastname}}" style="border:none" readonly></td>

    </tr>
    <tr>
        <td>FIRSTNAME</td>
        <td colspan="6"><input type="text" size="107" value="{{i.Firstname}}" style="border:none" readonly></td>
    </tr>
{% endfor %}
{%for i in studentenrolment%}
姓
名字
{%endfor%}

只需使用
注册

enroll = StudentsEnrollmentRecord(
    Student_Users=student,
    Old_Student=old,
    New_Student=new,
    Payment_Type=payment,
    ESC_Student=esc,
    Last_School_Attended=last,
    Address_OF_School_Attended=add,
    Education_Levels=educationlevel,
    School_Year=schoolyear,
    Courses=course,
    strands=strands,
    Student_Types=stype,
    GWA=gwa
)

enroll.save()

return render(request, "print.html", {"studentenrolment": enroll})
enroll=StudentsEnrollmentRecord(
学生用户=学生,
老学生,
新生,
付款类型=付款,
ESC_学生=ESC,
上次上学=上次,
学校地址=添加,
教育水平=教育水平,
学年=学年,
课程=课程,
股=股,
学生类型=stype,
GWA=GWA
)
注册。保存()
返回呈现(请求“print.html”,{“studentenrolment”:enroll})
#print.html
姓
名字

当我尝试此操作时,出现以下错误“”“'StudentsEnrollmentRecord'对象不可编辑”“”
enroll
是单个对象,不是查询集,因此它不是可编辑对象(不是类似列表的对象)。你可以显示你的
print.html
?我只是复制粘贴你的答案,但我收到了这个错误“”“'StudentsEnrollmentRecord'对象不可编辑”“”请检查我的更新问题,我只发布我的一些print.htmlt要消除错误,请删除for循环并使用
studentenroll
而不是
i
# print.html
<tr>
<tr>
    <td>SURNAME</td>
    <td colspan="6"><input type="text" size="107" value="{{ studentenrolment.Lastname }}" style="border:none" readonly>
    </td>

</tr>
<tr>
    <td>FIRSTNAME</td>
    <td colspan="6"><input type="text" size="107" value="{{ studentenrolment.Firstname }}" style="border:none" readonly>
    </td>
</tr>