Django技术开发--Django模型层(多表操作)

1、创建模型

示例:我们来假定下面这些概念,字段和关系

学生模型:一个学生有姓名、年龄

学生详细模型:把学生的详情放到详情表,包含生日,手机号,家庭住址等信息。学生详情模型和学生模型之间是一对一的关系(one-to-one)

学校信息:学校有名称、城市以及邮箱

课程模型:课程有场所和日期,一个课程可能会有多人喜欢,一个学生也可以喜欢多种课程,所以学生和课程是多对多的的关联关系(many-to-many);一个学校和课程应该一对多的关系(one-to-many)

(1)数据库创建

(2)模型创建models.py

#coding:utf-8
from django.db import models

# Create your models here.
#课程模型
class Course(models.Model):
    nid = models.AutoField(primary_key=True)
    #课程名字
    couse_name = models.CharField(max_length=32)
    #课程容量
    couse_num = models.IntegerField()
    #课程日期
    couse_date = models.DateTimeField()
    #课程学校
    school = models.ForeignKey(to="School",on_delete=models.CASCADE)#级联删除
    #课程学生
    student = models.ManyToManyField(to="Student")
    #让返回的是课程的名称
    def __str__(self):
        return self.couse_name

#学校模型
class School(models.Model):
    nid = models.AutoField(primary_key=True)
    #学校名称
    school_name = models.CharField(max_length=32)
    #学校邮箱
    school_email = models.CharField(max_length=32)
    #学校地址
    school_address = models.CharField(max_length=32)
        
#学生模型
class Student(models.Model):
    nid = models.AutoField(primary_key=True)
    #学生姓名
    stu_name = models.CharField(max_length=32)
    #学生年龄
    stu_age = models.IntegerField()
    #学生邮箱
    stu_email = models.CharField(max_length=32)
    def __str__(self):
        return self.stu_name
    
    
#学生信息模型
class StudentMessage(models.Model):
    #学生地址
    stu_addr = models.CharField(max_length=32)
    #学生电话
    stu_tel = models.IntegerField()
    def __str__(self):
        return self.stu_addr
    

此时进行数据库迁移生成多张表

(3)数据库迁移

(4) 设置course_views的页面

#coding:utf-8
from django.shortcuts import render
from app01.models import *


# Create your views here.

################课程管理系统##################
def course_view(request):
    #课程列表
    course_list = Course.objects.all()
    #返回课程管理页面
    return render(request,"course_view.html",{"course_list":course_list})

配置静态路径settings.py

STATIC_URL = '/static/'
#配置静态路径
STATICFILES_DIRS = [
    os.path.join(BASE_DIR,"static")
]

course_views.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>课程管理页面</title>
    <link rel="stylesheet" href="/static/bootstrap.css">
</head>
<body>
    <h3>课程管理页面</h3>
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <a href="">添加课程</a>
                <table class="table table-bordered table-hover table-striped">
                    <thead>
                        <tr>
                            <th>课程编号</th>
                            <th>课程名称</th>
                            <th>课程容量</th>
                            <th>课程日期</th>
                            <th>课程学校</th>
                            <th>课程学生</th>
                            <th>课程操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for course in course_list %}
                            <tr>
                                <td>{{ forloop.counter }}</td>
                                <td>{{ course.course_name }}</td>
                                <td>{{ course.course_num }}</td>
                                <td>{{ course.course_date|date:"Y-m-d" }}</td>
                                <td>{{ course.school.name }}</td>
                                <td>{% for student in course.students.all %}
                                    <span>{{ student.name }}</span>
                                    {% if not forloop.last %}
                                        ,
                                    {% endif %}
                                {% endfor %}
                                </td>
                                <td>
                                    <a href="">编辑</a>
                                    <a href="">删除</a>
                                </td>
                            </tr>
                        {% endfor %}

                    </tbody>
                </table>
            </div>

        </div>
    </div>
</body>
</html>

运行结果:

(5)

发布了78 篇原创文章 · 获赞 5 · 访问量 8310

猜你喜欢

转载自blog.csdn.net/qq_36789311/article/details/100066864