https://www.django-rest-framework.org/topics/documenting-your-api/
class ConsumerViewset(CreateModelMixin, mixins.UpdateModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
这时的url 应当为 user/id,一般的做法是
# permission_classes = (permissions.IsAuthenticated, )
但是我们已经复用了ConsumerViewset类,因为该类还实现了用户注册的功能,而注册时是不需要验证是否已经登录的
所有我们需要设置动态的permission
def get_permissions(self):
if self.action == "retrieve":
return [permissions.IsAuthenticated()]
elif self.action == "create":
return []
return []
补充:或者我们将user_id返回给前端,让前端带着id来请求url user/id
JSONWebTokenAuthentication 是使用JWT模式进行认证
SessionAuthentication 是使用Session模式进行认证,方便浏览器端的调试
authentication_classes = (JSONWebTokenAuthentication, authentication.SessionAuthentication )
class ConsumerDetailSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("name", "birthday", "mobile", "gender", "email")
def get_serializer_class(self):
if self.action == "retrieve":
return ConsumerDetailSerializer
elif self.action == "create":
return ConsumerRegSerializer
return ConsumerDetailSerializer
补充,小tips,get_object方法
# 当Retrieve和Update,Destroy的时候都会用到该方法
def get_object(self):
return self.request.user
因为个人收藏中要包含收藏的商品,而原来的serializer只会返回goods_id 和 id ,所有需要重写序列号类,用于返回收藏详情页的信息
class UserFavDetailSerializer(serializers.ModelSerializer):
goods = GoodsSerializer()
class Meta:
model = UserFav
field = ('goods','id')
因为goods本身就是外键,只会调用出一个对象出来,所有就会不用再设置many=True了,所有many=True,就相当于xxx_set的set,只有没有设置外键要反向引用的时候才会设置
# serializer_class = UserFavSerializer
def get_serializer_class(self):
if self.action == "list":
return UserFavDetailSerializer
elif self.action == "create":
return UserFavSerializer
return UserFavSerializer
小tips,serializers.HiddenField,隐藏了该字段,会做后端的操作,但不会序列号返回给前端
# 获取当前用户,并且隐藏了该字段,不会序列号返回给前端
user = serializers.HiddenField(
default=serializers.CurrentUserDefault()
)
class UserRemarkViewset(mixins.CreateModelMixin,mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,mixins.ListModelMixin,viewsets.GenericViewSet):
"""
list:
获取用户留言
create:
添加留言
delete:
删除留言功能
"""
queryset = UserRemark.objects.all()
serializer_class = UserRemarkSerializer
permission_classes = (IsAuthenticated,IsOwnerOrReadOnly)
authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
def get_queryset(self):
return UserRemark.objects.filter(user=self.request.user)
class UserRemarkSerializer(serializers.ModelSerializer):
# 获取当前用户,并且隐藏了该字段,不会序列号返回给前端
user = serializers.HiddenField(
default=serializers.CurrentUserDefault()
)
# read_only=True 这个值只返回给前端不让前端提交
# write_only=True 这个值只会提交,不会返回给前端
# format 设置日期格式
add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M')
class Meta:
model = UserRemark
fields = ("user", "message_type", "subject", "message", "file", "id" ,"add_time")
注意:
read_only=True 这个值只返回给前端,不让前端提交
write_only=True 这个值只会让前端提交,不会再返回给前端
class UserAddressViewset(viewsets.ModelViewSet):
"""
收货地址管理
list:
获取收货地址
create:
添加收货地址
update:
更新收货地址
delete:
删除收货地址
"""
queryset = UserAddress.objects.all()
serializer_class = UserAdressSerializer
permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
def get_queryset(self):
return UserAddress.objects.filter(user=self.request.user)
因为这里用到了 增删改查 四个接口,所有可以直接继承Modelviewset
class UserAdressSerializer(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")