1、创建单表模型类
class Goods(models.Model):
goods_name = models.CharField(max_length=32)
goods_price = models.DecimalField(max_digits=9,decimal_places=2)
goods_num = models.IntegerField()
class Meta:
db_table = 'tb_goods'
2、ORM基本操作
class ORMView(APIView):
def get(self,request):
# 单一查询,如果结果不存在报错
goodsobj = Goods.objects.get(goods_price=6.66)
obj = GoodsSerializers(goodsobj)
return Response(obj.data)
# # 查询不包含id=3的数据
# goodsobj = Goods.objects.exclude(id=3)
# obj = GoodsSerializers(goodsobj, many=True)
# return Response(obj.data)
# # 查询结果的数量
# goodsobj = Goods.objects.all().count()
# return Response(goodsobj)
# # 聚合函数 使用aggregate()过滤器调用聚合函数。聚合函数包括:Avg 平均,Count 数量,Max 最大,Min 最小,Sum 求和
# goodsobj = Goods.objects.aggregate(Sum('goods_num'))
# print(goodsobj)
# # obj = GoodsSerializers(goodsobj, many=True)
# return Response({'data':''})
# # 比较查询 字段名__lt:小于 字段名__lte:小于等于
# goodsobj = Goods.objects.filter(id__lt=4)
# obj = GoodsSerializers(goodsobj, many=True)
# return Response(obj.data)
# # 比较查询 字段名__gt:大于 字段名__gte:大于等于
# goodsobj = Goods.objects.filter(id__gte=2)
# obj = GoodsSerializers(goodsobj, many=True)
# return Response(obj.data)
# # 空查询 字段名__isnull
# goodsobj = Goods.objects.filter(goods_name__isnull=False)
# obj = GoodsSerializers(goodsobj, many=True)
# return Response(obj.data)
# # 模糊查询 字段名__endswith 以 莓 结尾的数据
# goodsobj = Goods.objects.filter(goods_name__endswith='莓')
# obj = GoodsSerializers(goodsobj, many=True)
# return Response(obj.data)
# # 模糊查询 字段名__tartswith 以 苹 开头的数据 # goodsobj = Goods.objects.filter(goods_name__startswith='苹') # obj = GoodsSerializers(goodsobj,many=True) # return Response(obj.data) # # 模糊查询 字段名__contains 包含 果 的的数据 # goodsobj = Goods.objects.filter(goods_name__contains='果') # obj = GoodsSerializers(goodsobj,many=True) # return Response(obj.data)
# # 范围查询: in 只查询列表中的具体值
# goodsobj = Goods.objects.filter(goods_price__in=[12,25,6.66]).all()
# obj = GoodsSerializers(goodsobj,many=True)
# return Response(obj.data)
# # 范围查询: range 1~20之间
# goodsobj = Goods.objects.filter(goods_price__range=[1,20]).all()
# obj = GoodsSerializers(goodsobj,many=True)
# return Response(obj.data)
# 排序查询: order_by 降序只需在字段名前加上 “ - ”
goodsobj = Goods.objects.all().order_by('-goods_num')
obj = GoodsSerializers(goodsobj, many=True)
return Response(obj.data)
# F方法:比较两个字段对象之间的关系用F对象,F方法可以进行简单运算
# 查询价格 大于等于 库存的对象
goodsobj = Goods.objects.filter(goods_price__gte=F('goods_num'))
obj = GoodsSerializers(goodsobj, many=True)
return Response(obj.data)
# Q方法:对对象进行复杂查询,并支持&(and),|(or),~(not)操作符
# 查询价格大于等于15的数据 或库存大于等于10的数据
goodsobj = Goods.objects.filter(Q(goods_price__gte=15) | Q(goods_num__gte=10))
obj = GoodsSerializers(goodsobj, many=True)
return Response(obj.data)
3、创建一对多、多对多模型类
# 电影
class Movie(models.Model):
movie_name = models.CharField(max_length=32)
class Meta:
db_table = 'tb_movie'
# 角色
class Role(models.Model):
role_name = models.CharField(max_length=32)
role_price = models.DecimalField(max_digits=9, decimal_places=2)
role_desc = models.CharField(max_length=32)
class Meta:
db_table = 'tb_role'
# 演员
class Actor(models.Model):
name = models.CharField(max_length=32)
address = models.CharField(max_length=32, null=True, blank=True)
age = models.IntegerField(null=True,blank=True)
phone = models.CharField(max_length=32, null=True, blank=True)
movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
role = models.ManyToManyField(Role)
class Meta:
db_table = 'tb_actor'
- 一对多正、反查找
# # 正向查找 对象.模型名.字段
# actorobj = Actor.objects.get(id=1)
# movieo = actorobj.movie.movie_name
# print(movieo)
# return Response('ok')
# 反向查找 对象.模型名_set
# movieobj = Movie.objects.get(id=1)
# actorobj = movieobj.actor_set.all()
# obj = ActorSerializers(actorobj,many=True)
# print(obj.data)
# return Response(obj.data)
4、多对多表操作
class ORMview2(APIView):
def post(self,request):
# # 多对多添加
# # 方法一:在建立manytomany的models里添加数据,(一条,一个对象)
# actor = Actor.objects.get(id=5)
# role = Role.objects.get(id=5)
#
# data = actor.role.add(role)
# print(data)
# return Response('ok')
# 方法二:在未建立manytomany的models里添加数据,(一条,一个对象)
actor = Actor.objects.filter(name='张译')
role = Role.objects.get(id=5)
data = role.actor_set.add(*actor)
print(data)
return Response('ok2')
5、父子组件的调用
import导入子组件
components注册子组件
注册的子组件当作标签来使用
- 子组件
<template> <div> <p>今天心情真美丽</p> </div></template>
-
父组件
<template> <div> <zi></zi> <!-- 子组件当标签使用--> </div> </template> <script> import zi from "./zi"; //导入子组件 export default { data() { return { }; }, //注册子组件 components: { zi }, } </script>
-
父组件给子组件传参
父组件里的子标签里写上要传递的数据(:子组件的参数名字=父组件的参数名字)
在子组件里注册参数(props)
使用父组件传过来的参数
<template>
<div>
<zi :str='str'></zi><br>
<br>
<button @click="change_zi()">修改子组件</button>
</div>
</template>
<script>
import zi from "./zi";
export default {
data() {
return {
str:'',
};
},
components: {
zi
},
methods:{
change_zi(){
this.str='今天的你真可爱!'
},
}
};
</script>
//子组件获取
<template>
<div>
<p>今天心情真美丽</p>
{{str}}
</div>
</template>
<script>
export default {
props:['str'],
data(){
return{
}
},
}
</script>
-
子组件给父组件传参
子组件里先用特定的方法来把这个数据传递给父组件(this.$emit(‘方法的名字’,要传递的数据))
父组件的子标签里来接收数据(@子组件的方法=父组件的方法)
methods里接收传过来的val并赋值
<template>
<div>
<p>今天心情真美丽</p>
<button @click="change">修改父组件</button>
</div>
</template>
<script>
export default {
data(){
return{
}
},
methods:{
change(){
this.$emit('change_fu','真好!nice!')
}
}
}
</script>
//父组件获取
<template>
<div>
<zi @change_fu=get_zi></zi><br>
{{data}}
</div>
</template>
<script>
import zi from "./zi";
export default {
data() {
return {
data:''
};
},
components: {
zi
},
methods:{
get_zi(val){
this.data=val
}
}
};
</script>