关系表
模型创建
查看数据库
增加
增加老师数据
查询
更改
删除
聚合查询 avg sum max min count
通过aggregate调用聚合函数
aggregate () 是一个queyset的方法,意思是:返回一个包含数据的键值对的字典
键名:聚合值的表示符号,值:聚合函数计算的结果。
def jttest(request):
data = Person.objects.all().aggregate(Avg("age"))
print(data)
data = Person.objects.all().aggregate(avg_age = Avg("age"),sum_age = Sum("age"))
print (data)
return HttpResponse("集合查询")
F对象 Q对象
F对象: 用于比较同一个模型中的两个字段的值
Q对象
用于处理多条件中的逻辑关系 and or not
def Qtest(request):
### and
data = Book.objects.filter(num=10,salled=100).all()
print (data)
# and 关系
data = Book.objects.filter(Q(num__gt=10)&Q(salled=100)).all()
print(data)
## or
data =Book.objects.filter(Q(num__gt=10)|Q(salled=100)).all()
print (data)
## not
data = Book.objects.filter(~Q(num=10)|~Q(salled=100)).all()
print (data)
return HttpResponse("Q obj test")
站点模型
注册超级用户
createsuperuser