63、ORM学员管理系统--Django框架之外键

学生信息管理

展示学生信息

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
'1@1.com'

反向查询(由学生详情表反向查询学生信息表)

>>> detail_obj = models.StudentDetail.objects.get(id=1)
>>> detail_obj.student.sname
'a'
posted @ 2018-04-08 15:30  王二黑  阅读(114)  评论(0编辑  收藏  举报