|
1 | | -# Create your views here. |
2 | | - |
3 | | - |
4 | | -from django_filters import rest_framework as filters |
5 | | -from rest_framework.decorators import action |
6 | | - |
7 | | -from common.core.filter import BaseFilterSet |
8 | | -from common.core.modelset import BaseModelSet, ImportExportDataAction |
9 | | -from common.core.pagination import DynamicPageNumber |
10 | | -from common.core.response import ApiResponse |
11 | | -from common.utils import get_logger |
12 | | -from demo.models import Book |
13 | | -from demo.serializers.book import BookSerializer |
14 | | - |
15 | | -logger = get_logger(__name__) |
16 | | - |
17 | | - |
18 | | -class BookViewSetFilter(BaseFilterSet): |
19 | | - name = filters.CharFilter(field_name='name', lookup_expr='icontains') |
20 | | - author = filters.CharFilter(field_name='author', lookup_expr='icontains') |
21 | | - publisher = filters.CharFilter(field_name='publisher', lookup_expr='icontains') |
22 | | - |
23 | | - class Meta: |
24 | | - model = Book |
25 | | - fields = ['name', 'isbn', 'author', 'publisher', 'is_active', 'publication_date', 'price', |
26 | | - 'created_time'] # fields用于前端自动生成的搜索表单 |
27 | | - |
28 | | - |
29 | | -class BookViewSet(BaseModelSet, ImportExportDataAction): |
30 | | - """书籍""" # 这个 书籍 的注释得写, 否则菜单中可能会显示null,访问日志记录中也可能显示异常 |
31 | | - |
32 | | - queryset = Book.objects.all() |
33 | | - serializer_class = BookSerializer |
34 | | - ordering_fields = ['created_time'] |
35 | | - filterset_class = BookViewSetFilter |
36 | | - pagination_class = DynamicPageNumber(1000) # 表示最大分页数据1000条,如果注释,则默认最大100条数据 |
37 | | - |
38 | | - @action(methods=['post'], detail=True) |
39 | | - def push(self, request, *args, **kwargs): |
40 | | - """推送到其他服务""" # 这个 推送到其他服务 的注释得写, 否则菜单中可能会显示null,访问日志记录中也可能显示异常 |
41 | | - |
42 | | - # 自定义一个请求为post的 push 路由行为,执行自定义操作, action装饰器有好多参数,可以查看源码自行分析 |
43 | | - instance = self.get_object() |
44 | | - return ApiResponse(detail=f"{instance.name} 推送成功") |
| 1 | +# Create your views here. |
| 2 | + |
| 3 | + |
| 4 | +from django_filters import rest_framework as filters |
| 5 | +from rest_framework.decorators import action |
| 6 | + |
| 7 | +from common.core.filter import BaseFilterSet, PkMultipleFilter |
| 8 | +from common.core.modelset import BaseModelSet, ImportExportDataAction |
| 9 | +from common.core.pagination import DynamicPageNumber |
| 10 | +from common.core.response import ApiResponse |
| 11 | +from common.utils import get_logger |
| 12 | +from demo.models import Book |
| 13 | +from demo.serializers.book import BookSerializer |
| 14 | + |
| 15 | +logger = get_logger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class BookViewSetFilter(BaseFilterSet): |
| 19 | + name = filters.CharFilter(field_name='name', lookup_expr='icontains') |
| 20 | + author = filters.CharFilter(field_name='author', lookup_expr='icontains') |
| 21 | + publisher = filters.CharFilter(field_name='publisher', lookup_expr='icontains') |
| 22 | + |
| 23 | + # 自定义的搜索模板,针对用户搜索,前端已经内置 api-search-user 模板处理 |
| 24 | + managers2 = PkMultipleFilter(input_type='api-search-user') |
| 25 | + |
| 26 | + # 自定义的搜索模板,默认是带有choices的下拉框,当数据多的话,体验不好,所以这里改为输入框,前端已经内置 input 处理 |
| 27 | + managers = PkMultipleFilter(input_type='input') |
| 28 | + |
| 29 | + |
| 30 | + class Meta: |
| 31 | + model = Book |
| 32 | + fields = ['name', 'isbn', 'author', 'publisher', 'is_active', 'publication_date', 'price', |
| 33 | + 'created_time', 'managers', 'managers2'] # fields用于前端自动生成的搜索表单 |
| 34 | + |
| 35 | + |
| 36 | +class BookViewSet(BaseModelSet, ImportExportDataAction): |
| 37 | + """书籍""" # 这个 书籍 的注释得写, 否则菜单中可能会显示null,访问日志记录中也可能显示异常 |
| 38 | + |
| 39 | + queryset = Book.objects.all() |
| 40 | + serializer_class = BookSerializer |
| 41 | + ordering_fields = ['created_time'] |
| 42 | + filterset_class = BookViewSetFilter |
| 43 | + pagination_class = DynamicPageNumber(1000) # 表示最大分页数据1000条,如果注释,则默认最大100条数据 |
| 44 | + |
| 45 | + @action(methods=['post'], detail=True) |
| 46 | + def push(self, request, *args, **kwargs): |
| 47 | + """推送到其他服务""" # 这个 推送到其他服务 的注释得写, 否则菜单中可能会显示null,访问日志记录中也可能显示异常 |
| 48 | + |
| 49 | + # 自定义一个请求为post的 push 路由行为,执行自定义操作, action装饰器有好多参数,可以查看源码自行分析 |
| 50 | + instance = self.get_object() |
| 51 | + return ApiResponse(detail=f"{instance.name} 推送成功") |
0 commit comments