查询表格中的数据(2):
from django.http import HttpResponse
1.或条件(引入函数模块Q)
stus = Student.objects.filter(Q(s_age=20) | Q(s_gender=1))
2.且条件
stus = Student.objects.filter(Q(s_age=20) & Q(s_gender=1))
3.非条件
stus = Student.objects.filter(~Q(s_age=20))
4.查询表中数据的大小
stus = Student.objects.filter(字段__gt=F('另一个字段'))
5.查询语文比数学多10分的同学
stus = Student.objects.filter(yuwen__gt=F('math') + 10)
stu_names = [stu.s_name for stu in stus]
return HttpResponse('查询成功')
删除列表中的数据:
def del_stu(request):
Student.objects.filter(s_name='旺财').delete()
return HttpResponse('删除成功')
更新列表中的数据
方法一:
def update_stu(request):
stu = Student.objects.filter(s_name'旺财').first()
stu.s_name = '王老五'
stu.save()
return HttpResponse('修改成功')
方法二:
def update_stu(request):
Student.objects.filter(s_name='被修改内容').update(s_name='修改内容')
return HttpResponse('修改成功')
在页面上获取所有学生的信息
from django.shortcuts import render
def all_stu(request):
stus = Student.objects.all()
return render(request, '页面文件名', 数据内容)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<thead>
<th>姓名</th>
<th>年龄</th>
<th>数学成绩</th>
<th>语文成绩</th>
<th>操作</th>
</thead>
{% for stu in students %}
<tbody>
<!--解析变量-->
<td>{{ stu.s_name }}</td>
<td>{{ stu.s_age }}</td>
<td>{{ stu.math }}</td>
<td>{{ stu.yuwen }}</td>
<td>
<a href="/add_info/?stu_id={{ stu.id }}">添加拓展信息</a>
</td>
</tbody>
{% endfor %}
</table>
</body>
</html>
url(r'^all_stu/', views.all_stu)
一对一的搜索
class StudentInfo(models.Model):
phone = models.CharField(max_length=11, null=True)
address = models.CharField(max_length=100)
stu = models.OneToOneField(Student)
class Meat
db_table = 'student_info'
def add_info (request):
if request.method = 'GET':
return render(request, 'info.html')
if request.method = 'POST':
phone = request.POST.get('phone')
address = request.POST.get('address')
stu_id = request.GET.get('stu_id')
StudentInfo.objects.create(phone=phone, address=address, stu_id=stu_id)
return HttpResponse('创建拓展表成功')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
电话号码:<input type="text" name="phone">
地址:<input type="text" name="address">
<input type="submit" value="提交">
</form>
</body>
</html>
url(r'^sel_info/', views.sel_info)
一对多的搜索
class Grade(models.Model):
g_name = models.CharField(max_length=10, unique=True)
class Meat:
db_table = 'grade'
class Student(models.Model):
s_name = models.CharField(max_length=6, unique=True)
s_age = models.IntegerField(default=18)
s_gender = models.BooleanField(default=1)
create_time = models.DateTimeField(auto_now_add=True, null=True)
operate_time = models.DateTimeField(auto_now=True, null=True)
yuwen = models.DecimalField(decimal_places=1, max_digits=4, null=True)
math = models.DecimalField(decimal_places=1, max_digits=4, null=True)
grade = models.ForeignKey(Grade, null=True)
def sel_info_by_stu(request):
if request.method == 'GET':
stu = Student.objects.get(s_name='小明')
info = StudentInfo.objects.filter(stu_id=stu.id)
info = StudentInfo.objects.filter(stu=stu)
info = stu.studentinfo
return HttpResponse('通过学生查找拓展表信息')
url(r'^sel_info_by_stu/', views.sel_info_by_stu),
def sel_stu_by_info(request):
if request.method == 'GET':
info = StudentInfo.objects.get(phone='12536524521')
student = info.stu
print(student)
return HttpResponse('通过手机号码查找学生信息')
url(r'^sel_stu_by_info/', views.sel_stu_by_info),
def add_grade(request):
if request.method == 'GET':
names = ['物联网', 'python', '外语']
for name in names:
Grade.objects.create(g_name=name)
return HttpResponse('创建班级成功')
url(r'^add_grade/', views.add_grade),
def sel_stu_grade(request):
if request.method == 'GET':
stu = Student.objects.filter(s_name='小明').first()
gradec = stu.grade
grade = Grade.objects.get(g_name='物联网')
student = grade.student_set.filter(s_gender=0).all()
return HttpResponse('查询学生和班级信息')
url(r'^sel_stu_grade/', views.sel_stu_grade),