Django+Vue打造购物网站(六)

商品详情页功能

商品详情页和CategoryViewSet类似,只需要多继承一个类(mixins.RetrieveModelMixin)就可以了

class GoodsListViewSet(mixins.ListModelMixin,mixins.RetrieveModelMixin, viewsets.GenericViewSet):

商品轮播图是一个外键,序列化外键用嵌套的方法来实现

# 轮播图
class GoodsImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsImage
        fields = ("image",)


# ModelSerializer实现商品列表页
class GoodsSerializer(serializers.ModelSerializer):
    # 覆盖外键字段
    category = CategorySerializer()
    # images是数据库中设置的related_name="images",把轮播图嵌套进来
    images = GoodsImageSerializer(many=True)

    class Meta:
        model = Goods
        fields = '__all__'

打开浏览器,找到一个商品打开,查看详情
数据应该已经填充进去了,如果商品详情中的图片未显示,
打开根目录下的proxy.js文件,将里面的url修改为本地即可

module.exports = {
  "/": "http://127.0.0.1:8000"
};

热卖商品接口实现

只需要在过滤器中增加is_hot就可以了

goods/filters.py里的GoodsFilter添加is_hot

    class Meta:
        model = Goods
        fields = ['pricemin', 'pricemax','is_hot']

在后台设置商品的is_hot为True,然后前端就可以显示出来了

用户收藏接口实现

user_operation/serializers.py

from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator

from user_operation.models import UserFav


class UserFavSerializer(serializers.ModelSerializer):
    # 获取当前登录的用户
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )

    class Meta:
        # validate实现唯一联合,一个商品只能收藏一次
        validators = [
            UniqueTogetherValidator(
                queryset=UserFav.objects.all(),
                fields=('user', 'goods'),
                # message的信息可以自定义
                message="该商品已经收藏"
            )
        ]
        model = UserFav
        # 收藏的时候需要返回商品的id,因为取消收藏的时候必须知道商品的id是多少
        fields = ("user", "goods", 'id')

user_operation/views.py

from rest_framework import viewsets, mixins

from user_operation.models import UserFav
from user_operation.serializers import UserFavSerializer


class UserFavViewset(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.CreateModelMixin, mixins.DestroyModelMixin):
    '''
    list:
        获取用户的所有收藏
    create:
        添加收藏
    destroy:
        取消收藏
    '''
    queryset = UserFav.objects.all()
    serializer_class = UserFavSerializer

配置url

# 配置用户收藏的url
router.register(r'userfavs', UserFavViewset, base_name="userfavs")

进行测试
当重复收藏某个商品时,会报错


Django+Vue打造购物网站(六)_第1张图片
image

drf的权限认证

http://www.django-rest-framework.org/api-guide/permissions/
utils文件夹下新建permissions.py

from rest_framework import permissions


class IsOwnerOrReadOnly(permissions.BasePermission):
    """
    Object-level permission to only allow owners of an object to edit it.
    Assumes the model instance has an `owner` attribute.
    """

    def has_object_permission(self, request, view, obj):
        # Read permissions are allowed to any request,
        # so we'll always allow GET, HEAD or OPTIONS requests.
        if request.method in permissions.SAFE_METHODS:
            return True

        # Instance must have an attribute named `owner`.
        return obj.user == request.user

user_operation/views.py

class UserFavViewset(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.CreateModelMixin, mixins.DestroyModelMixin):
    '''
    list:
        获取用户的所有收藏
    create:
        添加收藏
    destroy:
        取消收藏
    '''
    serializer_class = UserFavSerializer
    # permission是用来做权限判断的
    # IsAuthenticated:必须登录用户;IsOwnerOrReadOnly:必须是当前登录的用户
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
    # auth使用来做用户认证的
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    # 搜索的字段
    lookup_field = 'goods_id'

    def get_queryset(self):
        # 只能查看当前登录用户的收藏,不会获取所有用户的收藏
        return UserFav.objects.filter(user=self.request.user)

只有登录用户才可以收藏
用户只能获取自己的收藏,不能获取所有用户的收藏
JSONWebTokenAuthentication认证不需要全局配置
删除settings中的'rest_framework_jwt.authentication.JSONWebTokenAuthentication',

将前端代码中的地址替换为本地,进行测试

搜索字段是在获取当前用户后操作的,
也就是说搜索的是当前用户的收藏,
而不是在所有用户中进行搜索,

    def get_object(self):
        """
        Returns the object the view is displaying.

        You may want to override this if you need to provide non-standard
        queryset lookups.  Eg if objects are referenced using multiple
        keyword arguments in the url conf.
        """
        queryset = self.filter_queryset(self.get_queryset())

        # Perform the lookup filtering.
        lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field

收藏功能结束

你可能感兴趣的:(Django+Vue打造购物网站(六))