zoukankan      html  css  js  c++  java
  • 多表查询实例

    查询练习

    2.models.py 创建表关系

    from django.db import models
    
    # Create your models here.
    
    # 1.老师表
    class Teacher(models.Model):
    
        tid =  models.AutoField(primary_key=True)
        tname = models.CharField(max_length=32)
    
        class Meta:
            db_table="teacher"
        def __str__(self):
            return self.tname
    
    # 2.课程表
    class Course(models.Model):
        cid =  models.AutoField(primary_key=True)
        cname = models.CharField(max_length=32)
        #老师和课程 一对多
        teaher =models.ForeignKey(to="Teacher",on_delete=models.CASCADE)
        class Meta:
            db_table="course"
    
    #3. 年纪表
    class Class_grade(models.Model):
        gid = models.AutoField(primary_key=True)
        gname = models.CharField(max_length=32)
        class Meta:
            db_table="grade"
    
    
    #4.班级表
    class Class(models.Model):
        cid = models.AutoField(primary_key=True)
        caption = models.CharField(max_length=32)
        #年级和班级是一对多关系
        calss_grade = models.ForeignKey(to="Class_grade", on_delete=models.CASCADE)
        #班级和老师多对多
        teahcer=models.ManyToManyField(to="Teacher")
        class Meta:
            db_table="class"
    
    #5.学生表
    class Student(models.Model):
        sid = models.AutoField(primary_key=True)
        sname = models.CharField(max_length=32)
        gender = models.CharField(max_length=32)
        #学生表和班级表 多对一
        cls= models.ForeignKey(to="Class",on_delete=models.CASCADE)
        class Meta:
            db_table="student"
    
    
    
    #6.成绩表
    class Score(models.Model):
        sid = models.AutoField(primary_key=True)
        score =models.IntegerField()
    
        # #成绩和课程一对多
        #
        course =models.ForeignKey(to="Course", on_delete=models.CASCADE)
        # #成绩和学生一对多
        student = models.ForeignKey(to="Student", on_delete=models.CASCADE)
        class Meta:
            db_table="score"
        #
    表关系
  • 相关阅读:
    hdu 1325 判断有向图是否为树
    poj 1182
    Ubuntu 系统 文件操作命令
    vim 快捷键大全
    Git 上传本地命令
    git错误:fatal: Not a git repository (or any of the parent directories): .git
    Git 如何回到过去,然后 再 回到将来
    Git 提供篇
    Linux 的cp命令
    Linux :: vi E212: Can't open file for writing
  • 原文地址:https://www.cnblogs.com/knighterrant/p/10241191.html
Copyright © 2011-2022 走看看