總體邏輯相似於 我的中心 ( 我的中心的相關邏輯梳理詳情 點擊這裏 )html
也是兩個序列化組價的分流spa
查看收藏 ( list ) 詳情指向 收藏詳情 的組價rest
建立收藏 ( create ) 指向 用戶收藏 的組件code
收藏詳情須要用到收藏的商品所有內容, 序列化的時候須要進行組件的嵌套, 將商品序列化組件嵌進去orm
# 收藏詳情 class UserFavDetailSerializer(serializers.ModelSerializer): goods = GoodsSerializer(many=True) class Meta: model = UserFav fields = ("goods", "id")
收藏的時候須要注意設置一個聯合惟一索引, 在序列化中設置後或者 models 中設置皆可htm
# 用戶收藏 class UserFavSerializer(serializers.ModelSerializer): user = serializers.HiddenField( default=serializers.CurrentUserDefault() ) class Meta: model = UserFav # 聯合惟一能夠在 model 中建立的時候進行操做, 經過 ModelSerializer 天然會幫你完成驗證 # 也能夠在這裏完成, 注意是在 Meta 中進行設置, 由於這是多字段的處理 validators = [ UniqueTogetherValidator( queryset=UserFav.objects.all(), fields=('user', 'goods'), message="已經收藏過了" ) ] fields = ("user", "goods", "id") # 刪除的須要所以加上 id, 這樣方便刪除操做
總體邏輯相似於我的中心blog
依舊是 分流處理序列化組件, 由於收藏功能的所有請求都須要在用戶登陸下查看索引
所以不需認證分流utf-8
# 用戶收藏功能 class UserFavViewset(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, mixins.DestroyModelMixin, viewsets.GenericViewSet): """ list: 獲取用戶收藏列表 retrieve: 判斷某個商品是否已經收藏 create: 收藏商品 destroy: 取消商品收藏 """ # queryset = UserFav.objects.all() # 只能看本身的收藏記錄 permission_classes = (IsAuthenticated, IsOwnerOrReadOnly) serializer_class = UserFavSerializer authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication) lookup_field = "goods_id" # 分流處理 序列化組件 def get_serializer_class(self): if self.action == "list": return UserFavDetailSerializer elif self.action == "create": return UserFavSerializer return UserFavSerializer def get_queryset(self): return UserFav.objects.filter(user=self.request.user)
基本上所用的字段什麼的在上面都有用過了ci
須要注意的是留言時間的格式, 以及只讀屬性
注意 user 字段的傳入應該是默認的當前用戶, 且此字段能夠設置成隱藏
# 用戶留言 class LeavingMessageSerializer(serializers.ModelSerializer): # 設置隱藏字段 user = serializers.HiddenField( # 默認值爲當前用戶 default=serializers.CurrentUserDefault() ) # 留言的時間不能本身指定, 應該是系統自動根據當前的時間, 所以設置爲只讀 add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M') class Meta: model = UserLeavingMessage fields = ("user", "message_type", "subject", "message", "file", "id", "add_time")
# 用戶留言 class LeavingMessageViewset(mixins.ListModelMixin, mixins.DestroyModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet): """ list: 獲取用戶留言 create: 添加留言 delete: 刪除留言功能 """ permission_classes = (IsAuthenticated, IsOwnerOrReadOnly) authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication) serializer_class = LeavingMessageSerializer def get_queryset(self): return UserLeavingMessage.objects.filter(user=self.request.user)
基本上和留言同樣了.
# 收貨地址 class AddressSerializer(serializers.ModelSerializer): user = serializers.HiddenField( default=serializers.CurrentUserDefault() ) add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M') class Meta: model = UserAddress fields = ("id", "user", "province", "city", "district", "address", "signer_name", "add_time", "signer_mobile")
# 收貨地址 class AddressViewset(viewsets.ModelViewSet): """ 收貨地址管理 list: 獲取收貨地址 create: 添加收貨地址 update: 更新收貨地址 delete: 刪除收貨地址 """ permission_classes = (IsAuthenticated, IsOwnerOrReadOnly) authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication) serializer_class = AddressSerializer def get_queryset(self): return UserAddress.objects.filter(user=self.request.user)
# _*_ coding:utf-8 _*_ from rest_framework.validators import UniqueTogetherValidator from goods.serializers import GoodsSerializer __author__ = "yangtuo" __date__ = "2019/4/18 20:31" from rest_framework import serializers from .models import UserFav, UserLeavingMessage, UserAddress # 收藏詳情 class UserFavDetailSerializer(serializers.ModelSerializer): goods = GoodsSerializer() class Meta: model = UserFav fields = ("goods", "id") # 用戶收藏 class UserFavSerializer(serializers.ModelSerializer): # 用戶就使用當前的默認用戶, 而且設置爲隱藏 user = serializers.HiddenField( default=serializers.CurrentUserDefault() ) class Meta: model = UserFav # 聯合惟一能夠在 model 中建立的時候進行操做, 經過 ModelSerializer 天然會幫你完成驗證 # 也能夠在這裏完成, 注意是在 Meta 中進行設置, 由於這是多字段的處理 validators = [ UniqueTogetherValidator( queryset=UserFav.objects.all(), fields=('user', 'goods'), message="已經收藏過了" ) ] fields = ("user", "goods", "id") # 刪除的須要所以加上 id, 這樣方便刪除操做 # 用戶留言 class LeavingMessageSerializer(serializers.ModelSerializer): user = serializers.HiddenField( default=serializers.CurrentUserDefault() ) # 留言的時間不能本身指定, 應該是系統自動根據當前的時間, 所以設置爲只讀 add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M') class Meta: model = UserLeavingMessage fields = ("user", "message_type", "subject", "message", "file", "id", "add_time") # 收貨地址 class AddressSerializer(serializers.ModelSerializer): user = serializers.HiddenField( default=serializers.CurrentUserDefault() ) add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M') class Meta: model = UserAddress fields = ("id", "user", "province", "city", "district", "address", "signer_name", "add_time", "signer_mobile")
# _*_ coding:utf-8 _*_ from rest_framework.validators import UniqueTogetherValidator from goods.serializers import GoodsSerializer __author__ = "yangtuo" __date__ = "2019/4/18 20:31" from rest_framework import serializers from .models import UserFav, UserLeavingMessage, UserAddress # 收藏詳情 class UserFavDetailSerializer(serializers.ModelSerializer): goods = GoodsSerializer() class Meta: model = UserFav fields = ("goods", "id") # 用戶收藏 class UserFavSerializer(serializers.ModelSerializer): user = serializers.HiddenField( default=serializers.CurrentUserDefault() ) class Meta: model = UserFav # 聯合惟一能夠在 model 中建立的時候進行操做, 經過 ModelSerializer 天然會幫你完成驗證 # 也能夠在這裏完成, 注意是在 Meta 中進行設置, 由於這是多字段的處理 validators = [ UniqueTogetherValidator( queryset=UserFav.objects.all(), fields=('user', 'goods'), message="已經收藏過了" ) ] fields = ("user", "goods", "id") # 刪除的須要所以加上 id, 這樣方便刪除操做 # 用戶留言 class LeavingMessageSerializer(serializers.ModelSerializer): user = serializers.HiddenField( default=serializers.CurrentUserDefault() ) # 留言的時間不能本身指定, 應該是系統自動根據當前的時間, 所以設置爲只讀 add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M') class Meta: model = UserLeavingMessage fields = ("user", "message_type", "subject", "message", "file", "id", "add_time") # 收貨地址 class AddressSerializer(serializers.ModelSerializer): user = serializers.HiddenField( default=serializers.CurrentUserDefault() ) add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M') class Meta: model = UserAddress fields = ("id", "user", "province", "city", "district", "address", "signer_name", "add_time", "signer_mobile")