# app/goods/serializers.py
class BannerSerializer(serializers.ModelSerializer):
'''
轮播图
'''
class Meta:
model = Banner
fields = "__all__"
# app/goods/views.py
class BannerViewset(mixins.ListModelMixin, viewsets.GenericViewSet):
"""
首页轮播图
"""
queryset = Banner.objects.all().order_by("index")
serializer_class = BannerSerializer
# 配置首页轮播图的url
router.register(r'banners', BannerViewset, basename="banners
# app/goods/filters.py
class GoodsFilter(django_filters.rest_framework.FilterSet):
# ........
class Meta:
model = Goods
fields = ['price_min', 'price_max', 'top_category', 'is_hot', 'is_new']
# app/goods/views.py
class GoodsListViewSet(.....):
# .........
#商品点击数 + 1
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
instance.click_num += 1
instance.save()
serializer = self.get_serializer(instance)
return Response(serializer.data)
# app/user_operation/view.py
class UserFavViewset(...):
# .......
# 用户收藏的商品数量+1
def perform_create(self, serializer):
# 对象: UserFav商品收藏的对象
instance = serializer.save()
# 获取用户收藏的商品对象
goods = instance.goods
# 更新收藏商品的收藏数
goods.fav_num += 1
# 商品信息修改后保存到数据库中
goods.save()
# app/trade/views.py
class ShoppingCartViewset(viewsets.ModelViewSet):
# ..........
# 库存数-1
def perform_create(self, serializer):
shop_cart = serializer.save()
goods = shop_cart.goods
goods.goods_num -= shop_cart.nums
goods.save()
# 库存数+1
def perform_destroy(self, instance):
goods = instance.goods
goods.goods_num += instance.nums
goods.save()
instance.delete()
# 更新库存,修改可能是增加也可能是减少
def perform_update(self, serializer):
#首先获取修改之前的库存数量
existed_record = ShoppingCart.objects.get(id=serializer.instance.id)
existed_nums = existed_record.nums
# 先保存最新的购物车信息existed_nums
saved_record = serializer.save()
#变化的数量
nums = saved_record.nums-existed_nums
goods = saved_record.goods
goods.goods_num -= nums
goods.save()
class AlipayView(APIView):
def post(self, request):
# ...........
for existed_order in existed_orders:
# 订单商品项
order_goods = existed_order.goods.all()
# 商品销量增加订单中数值
for order_good in order_goods:
goods = order_good.goods
goods.sold_num += order_good.goods_num
goods.save()
# ............
1)安装Redis
yum install redis -y
bind 0.0.0.0
3)启动Redis服务
systemctl start redis
systemctl enable redis
systemctl status redis
4)查看Redis开启的端口号和绑定的IP
# Redis默认端口号6379
netstat -antlpe | grep redis
redis-cli -h IP
> ping
PONG
2.3缓存测试
class GoodsListViewSet(.....):
# .......
# Cache requested url for each user for 2 hours
@method_decorator(cache_page(60 * 60 * 2))
@method_decorator(vary_on_cookie)
def retrieve(self, request, *args, **kwargs):
redis-cli -h IP
> select 1
> keys *
> get keyname
2.4Throttle网站限速
REST_FRAMEWORK = {
#限速设置
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.AnonRateThrottle', #未登陆用户
'rest_framework.throttling.UserRateThrottle' #登陆用户
),
'DEFAULT_THROTTLE_RATES': {
'anon': '3/minute', #每分钟可以请求3次
'user': '5/minute' #每分钟可以请求五次
}
}
1)视图类
from rest_framework.throttling import UserRateThrottle,AnonRateThrottle
class GoodsListViewSet(......):
# ....
git p
2)API测试