Common skills of Python Django REST framework

Posted by cmccomas on Thu, 02 Apr 2020 06:27:41 +0200

Conditional filtering

Common filtration

_exact equals like 'aaa'
__iexact is exactly the same as ignoring the case ilike 'aaa'
__Contains contains like '% aaa%'
__Icontains includes ignore case ilike '% aaa%', but for sqlite, contains has the same effect as icontains.
__gt greater than
__gte greater than or equal to
__lt less than
__lte is less than or equal to
__In exists in a list range
__Startswitch to Start
__Istartswitch to Ignore case at beginning
__Endswitch to Ending
__Friends with End, ignore case
__range in Within limits
__Year year of the year date field
__Month the month of the date field
__Day the day of the date field
__isnull=True/False

Example

class GoodsListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
    """
    //Product list page
    """
    # Filter the price greater than 100 to return
    queryset = Goods.objects.all().filter(shop_price__gt=100)
    serializer_class = GoodsSerializer
    pagination_class = Pagination

Interface usage

Multilevel list

Three level list, the default ModelSerializer does not meet the requirements

class CategorySerializer3(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = "__all__"


class CategorySerializer2(serializers.ModelSerializer):
    sub_cat = CategorySerializer3(many=True)
    class Meta:
        model = GoodsCategory
        fields = "__all__"


class CategorySerializer(serializers.ModelSerializer):
    sub_cat = CategorySerializer2(many=True)
    class Meta:
        model = GoodsCategory
        fields = "__all__"

get find default non ID

class UpdatesTinkerViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    """
    Tinker Hot update list
    """
    queryset = UpdateTinker.objects.all()
    serializer_class = UpdatesSerializer
    # If not specified, the default is id
    lookup_field = "tinkerId"

Different api versions are not used

http://www.django-rest-framework.org/api-guide/versioning/#varying-behavior-based-on-the-version

def get_serializer_class(self):
    if self.request.version == 'v1':
        return AccountSerializerVersion1
    return AccountSerializer

Browse times

class GoodsListViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    # When the get method is called, the number of clicks + 1
    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)

Filter current user

Show only the list of current users

def get_queryset(self):
    return UpdateTinker.objects.filter(user=self.request.user)

Multi parameter use

class UpdatesTinkerFilter(FilterSet):
    """
    //filter of goods
    """
    applicationId = filters.CharFilter(name="applicationId", lookup_expr='iexact')
    version = filters.CharFilter(name="version", lookup_expr='iexact')

    class Meta:
        model = UpdateTinker
        fields = ['applicationId', 'version']

class UpdatesTinkerViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    """
    Tinker Hot update list
    """
    queryset = UpdateTinker.objects.all()
    serializer_class = UpdatesSerializer
    pagination_class = UpdatesTinkerPagination
    filter_backends = (DjangoFilterBackend,)
    search_fields = ('applicationId', 'version')
    # Find filter class
    filter_class = UpdatesTinkerFilter
    # Default search tinkerId
    lookup_field = "tinkerId"

Topics: hot update less SQLite Django