码迷,mamicode.com
首页 > 其他好文 > 详细

Django再次练习orm语句

时间:2018-10-14 00:21:45      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:style   end   contain   core   choices   pre   obj   ber   cas   

请配合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

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)

Django再次练习orm语句

标签:style   end   contain   core   choices   pre   obj   ber   cas   

原文地址:https://www.cnblogs.com/Guishuzhe/p/9784439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!