ORM版學員管理系統2
阿新 • • 發佈:2018-03-29
AD border 表結構 edi http 姓名 ger app 對象
學生信息管理
展示學生信息
URL部分
url(r‘^student_list/‘, app01_views.student_list, name="student_list"),
視圖部分
def student_list(request): student_list = models.Student.objects.all() return render(request, "student_list.html", {"student_list": student_list})
前端部分
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>學生列表</title> </head> <body> <a href="{% url ‘add_student‘ %}">添加學生</a> <table border="1"> <thead> <tr> <th>#</th> <th>id</th> <th>學生姓名</th> <th>學生班級</th> <th>開班日期</th> <th>操作</th> </tr> </thead> <tbody> {% for student in student_list%} <tr> <td>{{ forloop.counter}}</td> <td>{{ student.id }}</td> <td>{{ student.sname }}</td> <td>{{ student.cid.cname }}</td> <td>{{ student.cid.first_day|date:‘Y-m-d‘ }}</td> <td> <a href="{% url ‘delete_student‘ student.id %}">刪除</a> <a href="{% url ‘edit_student‘ student.id %}">編輯</a> </td> </tr> {% endfor %} </tbody> </table> </body> </html>
刪除學生信息
URL部分
url(r‘^delete_student/(?P<sid>\d+)$‘, app01_views.delete_student, name="delete_student"),
視圖部分
def delete_student(request, sid): models.Student.objects.filter(id=sid).delete() return redirect(reverse("student_list"))
前端部分
<a href="{% url ‘delete_student‘ student.id %}">刪除</a>
添加學生信息
URL部分
url(r‘^add_student/$‘, app01_views.add_student, name="add_student"),
視圖部分
def add_student(request): if request.method == "POST": sname = request.POST.get("sname") class_id = request.POST.get("class_id") models.Student.objects.create(sname=sname, cid_id=class_id) return redirect(reverse("student_list")) class_list = models.Class.objects.all() return render(request, "add_student.html", {"class_list": class_list})
前端部分
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>添加學生</title> </head> <body> <form action="{% url ‘add_student‘ %}" method="post"> {% csrf_token %} <p>學生姓名:<input type="text" name="sname"></p> <label for="class_id">所在班級:</label> <select name="class_id" id="class_id"> {% for class in class_list %} <option value="{{ class.id }}">{{ class.cname }}</option> {% endfor %} </select> <p><input type="submit" value="提交"></p> </form> </body> </html>
編輯學生信息
URL部分
url(r‘^edit_student/(?P<sid>\d+)$‘, app01_views.edit_student, name="edit_student"),
視圖部分
def edit_student(request, sid): # 獲取到編輯的學生對象 student_obj = models.Student.objects.get(id=sid) # 獲取所有的班級數據 class_list = models.Class.objects.all() if request.method == "POST": sname = request.POST.get("sname") class_id = request.POST.get("class_id") student_obj.sname = sname student_obj.cid_id = class_id student_obj.save() return redirect(reverse("student_list")) return render(request, "edit_student.html", {"student": student_obj, "class_list": class_list})
前端部分
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>編輯學生信息</title> </head> <body> <form action="{% url ‘edit_student‘ student.id %}" method="post"> {% csrf_token %} <p>學生姓名:<input type="text" name="sname" value="{{ student.sname }}"></p> <label for="class_id">所在班級:</label> <select name="class_id" id="class_id"> {% for class in class_list %} {% if class.id == student.cid_id %} <option value="{{ class.id }}" selected>{{ class.cname }}</option> {% else %} <option value="{{ class.id }}">{{ class.cname }}</option> {% endif %} {% endfor %} </select> <p><input type="submit" value="提交"></p> </form> </body> </html>
基於對象的跨表查詢
一對多查詢(班級表和學生表)
正向查詢(由學生表查詢班級表)
查詢學生的班級信息
>>> student_obj = models.Student.objects.first() >>> student_obj.cid # 通過model類中的屬性查找到對應的外鍵數據對象 <Class: Class object> >>> student_obj.cid.cname ‘1班‘ >>> student_obj.cid_id # 獲取實際外鍵的值 1
反向查詢(由班級表查詢學生表)
查詢班級的學生信息
>>> class_obj = models.Class.objects.first() # 獲取第一個班級對象 >>> class_obj.student_set.all() # 通過表名_set反向查詢出所有的學生 <QuerySet [<Student: Student object>, <Student: Student object>]>
註意:
如果不在外鍵的字段中設置related_name的話,默認就用表名_set。
如果設置了related_name="students",反向查詢時可直接使用students進行反向查詢。
>>> class_obj.students.all()
一對一查詢
表結構設計
class Student(models.Model): sname = models.CharField(max_length=32, verbose_name="學生姓名") the_class = models.ForeignKey(to=Class, to_field="id", on_delete=models.CASCADE, related_name="students") detail = models.OneToOneField(to="StudentDetail", null=True) class StudentDetail(models.Model): height = models.PositiveIntegerField() weight = models.PositiveIntegerField() email = models.EmailField()
正向查詢(由學生信息表查詢學生詳情表)
>>> student_obj = models.Student.objects.first() >>> student_obj.detail.email ‘[email protected]‘
反向查詢(由學生詳情表反向查詢學生信息表)
>>> detail_obj = models.StudentDetail.objects.get(id=1) >>> detail_obj.student.sname ‘a‘
ORM版學員管理系統2