• Django再次练习orm语句


    请配合https://www.cnblogs.com/Guishuzhe/p/9524790.html使用

    models.py

    from django.db import models
    
    
    class ClassTable(models.Model):
        """班级"""
        id = models.AutoField(primary_key=True)
        caption = models.CharField(max_length=32)
    
        def __str__(self):
            return self.caption
    
    
    class Student(models.Model):
        """学生"""
        id = models.AutoField(primary_key=True)
        name = models.CharField(max_length=32, verbose_name="学生名字")
        gender_choices = ((1, ""), (2, ""))
        gender = models.IntegerField(choices=gender_choices, verbose_name="性别")
        classb = models.ForeignKey(to="ClassTable", verbose_name="班级")
    
        def __str__(self):
            return self.name
    
    
    class Teacher(models.Model):
        """老师"""
        id = models.AutoField(primary_key=True)
        name = models.CharField(max_length=32, verbose_name="老师名字")
        students = models.ManyToManyField(to="Student")
        classb = models.ManyToManyField(to="ClassTable")
    
        def __str__(self):
            return self.name
    
    
    class Course(models.Model):
        """课程表"""
        id = models.AutoField(primary_key=True)
        cname = models.CharField(max_length=32, verbose_name="课程名称")
        teacher = models.ForeignKey(to="Teacher", verbose_name="老师", on_delete=models.CASCADE)
    
        def __str__(self):
            return self.cname
    
    
    class Score(models.Model):
        """成绩表"""
        id = models.AutoField(primary_key=True)
        number = models.IntegerField()
        student = models.ForeignKey(to="Student", verbose_name="学生")
        course = models.ForeignKey(to="Course", verbose_name="课程")
    
        def __str__(self):
            return self.number
    View Code

    xx.py
    from app01 import models
    from django.db.models import Avg, Sum, Max, Min, Count, F, Q, Value
    from django.db.models.functions import Concat
    
    # 查询体育课的成绩
    # val = models.Course.objects.values("score__number").filter(score__id=2)
    # # print(val)
    # 查询学生成绩高于80的人数
    # val = models.Course.objects.values("score__student").filter(score__number__gt=80)
    # print(val)
    # 查询生物课程的学生成绩
    # val = models.Student.objects.filter(score__course__cname="生物").values("score__number")
    # val = models.Course.objects.filter(id=1).values("score__number")
    # 查询物理课程的学生成绩
    # val = models.Course.objects.filter(id=3).values("teacher__students").values("score__number")
    # 查询生物/物理课学生的平均成绩
    # val = models.Course.objects.filter(id=1).values("teacher__students").aggregate(Avg("score__number"))
    # val = models.Course.objects.filter(id=3).values("teacher__students").aggregate(Avg("score__number"))
    # 分别查询所有课程平均成绩和最大最小成绩
    # val = models.Course.objects.all().aggregate(Avg("score__number"), Max("score__number"), Min("score__number"))
    # 查询每个部门的平均成绩
    # val = models.Course.objects.values("cname").annotate(avg=Avg("score__number")).values("cname", "avg")
    # 按课程查询每个课程的最大分数和最小分数
    # val = models.Course.objects.values("cname").annotate(max=Max("score__number"), min=Min("score__number")).values("cname", "max", "min")
    # 查询老师下面的所有学生的平均成绩
    # val = models.Teacher.objects.values("students").annotate(avg=Avg("students__score__number")).values("students", "avg")
    # 查询每个班级的平均分数
    # val = models.ClassTable.objects.values("caption").annotate(avg=Avg("student__score__number")).values("caption", "avg")
    # 查询班级学生名字叫钢蛋或者小王八的班级
    # val = models.ClassTable.objects.filter(Q(student__name="钢蛋")|Q(student__name="小王八"))
    # 查询班级上名字叫小五并且老师不是波多的班级
    # val = models.ClassTable.objects.filter(Q(student__name="小五") & ~Q(teacher__name="波多")).values_list("caption")
    # 查询课程是生物或课程名字带物,教师名字到苍的老师("哈哈")
    # val = models.Teacher.objects.filter(Q(course__cname="生物") | Q(course__cname__contains="物"), name__contains="苍")
    # 把所有班级后面都加上(阳光小学)
    # val = models.ClassTable.objects.all().update(caption=Concat(F("caption"), Value("("), Value("阳光小学"), Value(")")))
    # 吧id=2的成绩提高22(支持+ - * /)
    val = models.Score.objects.filter(id=2).update(number=F("number")+22)
    print(val)
    View Code


  • 相关阅读:
    dd——留言板再加验证码功能
    怎样去除织梦版权信息中的Power by DedeCms
    数据结构和算法的选择
    数据结构和算法9——哈希表
    数据结构与算法8——二叉树
    数据结构与算法7——高级排序
    数据结构与算法6——递归
    数据结构和算法5——链表
    数据结构与算法4——栈和队列
    数据结构与算法3——简单排序(冒泡、选择、插入排序)
  • 原文地址:https://www.cnblogs.com/Guishuzhe/p/9784439.html
Copyright © 2020-2023  润新知