views.py
from django.db.models import *
from django.shortcuts import render
#引入学生student模型
from app.models import Student,StuInfo
from django.http import HttpResponse
def add_stu(request):
"""
#向数据库中的student表中插入数据
stu=Student()
stu.name="小明"
#向数据库中插入数据
stu.save()
"""
#学生类Student===>多个对象=======>写入数据(名字xx,年龄xx,性别x)
Student.objects.create(name="小明",gender=0,age=19)
Student.objects.create(name="小花", age=20)
Student.objects.create(name="林平之", age=28)
Student.objects.create(name="令狐冲", age=30)
Student.objects.create(name="任盈盈", age=18,gender=0)
#查询语文成绩比数学成绩大的学生信息
stus = Student.objects.filter(yuwen__gt=F("nath"))
#__gt大于=====>yuwen__gt=F("nath")
#F后面是指定哪个对象来比较
return HttpResponse("创建成功")
def del_stu(request):
#实现删除
#sql:delete from student where name="大锤"
#1.查询name = "大锤"的信息
Student.objects.filter(name='王大锤').delete()
return HttpResponse("删除成功")
def up_stu(request):
"""
#实现更新
#第一种方式
stus=Student.objects.filter(name="小明")
stu = stus[0]
stu.name="大明"
stu.save()
return HttpResponse("更新成功")
"""
stu= Student.objects.filter(name="小明").first()
stu.name = "大明"
stu.save()
Student.objects.filter(name="大明").update(name="丽歌")
def sel_stu(request):
#查询
stus = Student.objects.filter(age=18)
stus = Student.objects.all()
#查询
# get():方法是获取唯一的满足条件的对象,且查询的条件必须存在
# 如果查询条件不存在,则报错
# 如果查询的结果对于一个,则报错
stu = Student.objects.get(name="大明")
#获取不叫小明的学生
stus = Student.objects.exclude(name="大明")#取反的操作
#查询年龄是18的女生
stus = Student.objects.filter(age=18,gender=0)
#排序 order_by
#降序 -age 在sql中 age desc
#升序 age 在sql中 age asc
stus = Student.objects.all().order_by("-age")
#获取对象的值
stus_value = Student.objects.all().values()
#判断name=妲己的学生存不存在
stu = Student.objects.filter(name="妲己")
#查询姓名中包含"大"的学生信息
#contains:大小写敏感
#icontains:大小写不敏感
#类似于like"%小%"
stus = Student.objects.filter(name__contains="小")
#类似于like"%小"
stus = Student.objects.filter(name__endswith="小")
#类似于like"小%"
stus = Student.objects.filter(name__startswith="小")
#sql select = from xxx where id in
stus = Student.objects.filter(id__in=[1,2,3])
stus = Student.objects.filter(pk__in=[1,2,3])
#大于:__gt
#小于:__lt
stus = Student.objects.filter(age__lt=19)#小于
#聚合aggregate
avg_age = Student.objects.all().aggregate(Avg("age"))
print(avg_age)
sum_age = Student.objects.all().aggregate(Sum("age"))
print(sum_age)
#或者条件、非条件、并且条件
#查询age = 18或者gender = 1的学生信息
#Q()
# from django.db.models import Q
stus = Student.objects.filter(Q(age=18),Q(gender=1))
stus = Student.objects.filter(Q(age=18)| Q(gender=1))
stus = Student.objects.filter(Q(age=18) & Q(gender=1))
stus = Student.objects.filter(~Q(age=18)|Q(gender=1))
# 获取学生的姓名
stus_names = [stu.name for stu in stus]
# 查询语文成绩比数学成绩大的学生信息
stus = Student.objects.filter(yuwen__gt=F("nath"))
# 查询语文成绩比数学成绩大10分的学生信息
stus = Student.objects.filter(yuwen__gt=F("nath")+10)
return HttpResponse(stus_names)
def add_stu_info(request):
s_info = Student()
s_info.phone = "12134123123"
s_info.address = "天府新区"
s_info.stu = Student.objects.get(pk=5)
s_info.save()
return HttpResponse("创建拓展信息")
#通过学生去查学生信息
def sel_info_by_stu(request):
#第一种方法
stu = Student.objects.filter(name="大明").first()#通过学生类找到名字叫大明这个对象,可能有多个,first取到的是第一个
#通过学生的信息表类找到与之
stu_info = StuInfo.objects.filter(stu_id=stu.id).first()
phone = stu_info.phone
#第二种方法(反向查询)
stu_info = stu.stuinfo
phone = stu_info.phone
return HttpResponse(phone)
def sel_stu_by_info(request):
stu_info = StuInfo.objects.get(phone="12134123123")
stu = stu_info.stu
return HttpResponse('通过拓展表查询学生信息')
models.py
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=10,unique=True)
age = models.IntegerField(default=18)
gender = models.BooleanField(default=1)
#auto_now_add:创建数据时,默认create_time字段为当前时间
create_time = models.DateTimeField(auto_now_add=True,null=True)
#auto_now:修改数据,每次update学生信息时,修改该字段的时间为当前时间
operate_time = models.DateTimeField(auto_now=True,null=True)
yuwen = models.DecimalField(max_digits=3,decimal_places=1, null=True)
nath = models.DecimalField(max_digits=3,decimal_places=1, null=True)
class Meta:
db_table = "student"
class StuInfo(models.Model):
phone = models.CharField(max_length=11)
address = models.CharField(max_length=100)
#OneToOneField等价于Forieignkey,且约束nuique=True
stu = models.OneToOneField(Student)
class Meta:
db_table = "grade"