RetrieveModelMixin
这个是获取商品详情的功能
需要重载他,然后加入自己的逻辑
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data)
放到这里
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)
进入断点了
=================================
下面是收藏数
点击进去
重载
def perform_create(self, serializer):
instance = serializer.save()
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()
测试