新人学习django 是按照这个大佬的教程学习的
链接: https://www.cnblogs.com/derek1184405959/p/8733194.html.
很多步骤都是按照上面的教程学习的,这里只是记录一下自己学习的过程和一些错误
如果有人想要系统的学习,建议去上面那位大佬处学习
1.轮播图接口实现
(1)goods/serializer
class BannerSerializer(serializers.ModelSerializer):
'''
轮播图
'''
class Meta:
model = Banner
fields = "__all__"
(2)goods/views.py
class BannerViewset(mixins.ListModelMixin, viewsets.GenericViewSet):
"""
首页轮播图
"""
queryset = Banner.objects.all().order_by("index")
serializer_class = BannerSerializer
(3)url
# 配置首页轮播图的url
router.register(r'banners', BannerViewset, base_name="banners")
2. is_new 新品接口功能开发
实现这个接口只要在goods/filters/GoodsFilter里面添加一个过滤就可以了
class Meta:
model = Goods
fields = ['pricemin', 'pricemax','is_hot','is_new']
3.首页商品分类显示功能
(1)goods/serializers.py
class BrandSerializer(serializers.ModelSerializer):
'''
大类下面的宣传商标
'''
class Meta:
model = GoodsCategoryBrand
fields = "__all__"
class IndexCategorySerializer(serializers.ModelSerializer):
#某个大类的商标,可以有多个商标,一对多的关系
brands = BrandSerializer(many=True)
# good有一个外键category,但这个外键指向的是三级类,直接反向通过外键category(三级类),取某个大类下面的商品是取不出来的
goods = serializers.SerializerMethodField()
# 在parent_category字段中定义的related_name="sub_cat"
# 取二级商品分类
sub_cat = CategorySerializer2(many=True)
# 广告商品
ad_goods = serializers.SerializerMethodField()
def get_ad_goods(self, obj):
goods_json = {}
ad_goods = IndexAd.objects.filter(category_id=obj.id, )
if ad_goods:
#取到这个商品Queryset[0]
good_ins = ad_goods[0].goods
#在serializer里面调用serializer的话,就要添加一个参数context(上下文request),嵌套serializer必须加
# serializer返回的时候一定要加 “.data” ,这样才是json数据
goods_json = GoodsSerializer(good_ins, many=False, context={'request': self.context['request']}).data
return goods_json
#自定义获取方法
def get_goods(self, obj):
# 将这个商品相关父类子类等都可以进行匹配
all_goods = Goods.objects.filter(Q(category_id=obj.id) | Q(category__parent_category_id=obj.id) | Q(
category__parent_category__parent_category_id=obj.id))
goods_serializer = GoodsSerializer(all_goods, many=True, context={'request': self.context['request']})
return goods_serializer.data
class Meta:
model = GoodsCategory
fields = "__all__"
(2)goods/views.py
class IndexCategoryViewset(mixins.ListModelMixin, viewsets.GenericViewSet):
"""
首页商品分类数据
"""
# 获取is_tab=True(导航栏)里面的分类下的商品数据
queryset = GoodsCategory.objects.filter(is_tab=True, name__in=["生鲜食品", "酒水饮料"])
serializer_class = IndexCategorySerializer
(3)url
# 首页系列商品展示url
router.register(r'indexgoods', IndexCategoryViewset, base_name="indexgoods")
4.商品点击数和收藏数
(1)点击数
GoodsListViewSet其中继承了mixins.RetrieveModelMixin(获取商品详情)
我们只要重写他的retrieve方法就可以了
goods/views
#商品点击数 + 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)
(2)收藏数 用信号量实现
delete和create的时候django model都会发送一个信号量出来,用信号量的方式代码分离性更好
收藏数+1和-1
(1)user_operation/signal.py
# users_operation/signal.py
from django.db.models.signals import post_save,post_delete
from django.dispatch import receiver
from user_operation.models import UserFav
# post_save:接收信号的方式
#sender: 接收信号的model
@receiver(post_save, sender=UserFav)
def create_UserFav(sender, instance=None, created=False, **kwargs):
# 是否新建,因为update的时候也会进行post_save
if created:
goods = instance.goods
goods.fav_num += 1
goods.save()
@receiver(post_delete, sender=UserFav)
def delete_UserFav(sender, instance=None, created=False, **kwargs):
goods = instance.goods
goods.fav_num -= 1
goods.save()
(2)user_operation/apps.py
from django.apps import AppConfig
class UserOperationConfig(AppConfig):
name = 'user_operation'
verbose_name = "操作管理"
def ready(self):
import user_operation.signal
5.商品库存量的修改
库存数量
商品库存数量的行为:
新增商品到购物车
修改购物车数量
删除购物车记录
# 库存数-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 = ShoppingCar.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()
6.drf缓存设置
加速网站的访问速度,将一些数据放到缓存当中,取数据的时候首先去缓存中去,然后再去数据库中取
github上面的使用说明:http://chibisov.github.io/drf-extensions/docs/#caching
(1)安装
pip install drf-extensions
(2)使用方法
导入
from rest_framework_extensions.cache.mixins import CacheResponseMixin
在GoodsListViewSet中添加缓存功能
#CacheResponseMixin一定要放在第一个位置
class GoodsListViewSet(CacheResponseMixin,mixins.ListModelMixin, mixins.RetrieveModelMixin,viewsets.GenericViewSet):
设置过期时间,settings里面
#缓存配置
REST_FRAMEWORK_EXTENSIONS = {
'DEFAULT_CACHE_RESPONSE_TIMEOUT': 15*60 #5s过期,时间自己可以随便设定
}
7.drf配置redis缓存
安装
pip install django-redis
(2)配置settings
# redis缓存
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
8.drf 设置api的访问速率
官网使用说明:http://www.django-rest-framework.org/api-guide/throttling/
settings中配置
REST_FRAMEWORK = {
#限速设置
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.AnonRateThrottle', #未登陆用户
'rest_framework.throttling.UserRateThrottle' #登陆用户
),
'DEFAULT_THROTTLE_RATES': {
'anon': '50/minute', #每分钟可以请求50次
'user': '50/minute' #每分钟可以请求50次
}
}
这里设置成每分钟五十次,是为了方便调试
goods/views.py
在GoodsListViewSet添加上
throttle_classes = (UserRateThrottle, AnonRateThrottle)
如果访问api时发现以前的api传送的数据发生变化,比如下面的这种
需要更改vue中以前修改过的源码,删除掉response.data.results中的results
这可能是引入了缓存设置后,api传输数据的格式发生了变化