1. Commodity details
Interface address: http://127.0.0.1:8000/goods/1/
Compared with the previous product list, there is only one more ID of a single product, so you only need to add a RetrieveModelMixin to the GoodsListViewSet to obtain the product details:
class GoodsListViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet)
Product details rotation chart
goods/serializers.py:
class GoodsImageSerializer(serializers.ModelSerializer): """Rotation chart in product details""" class Meta: model = GoodsImage fields = ('image',) class GoodsSerializer(serializers.ModelSerializer): """ Product list page """ # Overwrite foreign key fields category = CategorySerializer() # The product rotation chart covers the foreign key field. The related field is used here_ name='images' images = GoodsImageSerializer(many=True) class Meta: model = Goods fields = '__all__'
The product rotation chart is a foreign key field. You only need to nest this field in the product list page.
2. Hot goods
Interface address: http://127.0.0.1:8000/goods/?is_hot=true
1. At goods / filters Add is to PY_ Hot field:
class Meta: model = Goods fields = ['pricemin', 'pricemax', 'top_category', 'is_hot']
2. xadmin sets whether the goods are hot goods in the background and can be displayed in the front end
3. User collection
User collections involve:
- Collect items: a record will be created in UserFav (using CreateModelMixin)
- Cancel favorites: delete records (using DestroyModelMixin)
- Get favorites list: viewing datasets (using ListModelMixin)
Requirements:
- Only the logged in user can collect, cancel and view the collection list
- The current user can only get his own collections and cannot view other users' collections
- Collected and can no longer be collected (prompt)
Interface address:
- Collection: http://127.0.0.1:8000/userfavs/ , post request, carrying parameters: {goods: "4"}
- Cancel collection: http://127.0.0.1:8000/userfavs/3/ , delete request
- Favorite list: http://127.0.0.1:8000/userfavs/ , get request
3.1 interface implementation
1. New 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 collections""" # Get current login user user = serializers.HiddenField( default=serializers.CurrentUserDefault() ) class Meta: # validate realizes the unique union. A commodity can only be collected once validators = [ UniqueTogetherValidator( queryset=UserFav.objects.all(), fields=('user', 'goods'), message="Already collected" # Custom prompt ) ] model = UserFav # Returns the item ID used to cancel the collection fields = ('user', 'goods', 'id')
2,user_operation/views.py:
class UserFavViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.CreateModelMixin, mixins.DestroyModelMixin): """ User commodity collection ListModelMixin: Favorite list CreateModelMixin: Collection DestroyModelMixin: Cancel (delete) the collection and delete the data in the database accordingly """ serializer_class = UserFavSerializer queryset = UserFav.objects.all()
3. Configure route mxshop / URLs py:
router.register(r'userfavs', UserFavViewSet, basename='userfavs') # User commodity collection
4. Test:
3.2 authority authentication
Permission authentication enables only logged in users to collect, cancel collection and view collection list.
1. New apps / 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): # Allow any request to read permissions if request.method in permissions.SAFE_METHODS: return True # obj is equivalent to model. Change owner to user return obj.user == request.user
2,user_operation/views.py:
class UserFavViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.CreateModelMixin, mixins.DestroyModelMixin): """ User commodity collection ListModelMixin: Favorite list CreateModelMixin: Collection DestroyModelMixin: Cancel (delete) the collection and delete the data in the database accordingly """ serializer_class = UserFavSerializer queryset = UserFav.objects.all() # IsAuthenticated: the user must be logged in; IsOwnerOrReadOnly: must be the currently logged in user permission_classes = (IsAuthenticated, IsOwnerOrReadOnly) # User authentication authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication) # Search field (good_id will be used to cancel the collection) lookup_field = 'goods_id' def get_queryset(self): # You can only view the collections of the currently logged in user. It is forbidden to obtain the collections of other users return UserFav.objects.filter(user=self.request.user)
Note: JWT should not be configured globally, because some pages do not need to be verified, so local verification is enough. Just comment out the relevant configuration in settings.