Django-rest-framework Multi-Criteria Query/pagination/multi-table JSON
Django-rest-framework a multi-conditional query needs to overwrite the Listapiview.get_queryset method, code example:
Def get_queryset (self): "" " use Request.query_params to implement a multi-condition query, You can also use django filter , the simpler method is to specify the fields to filter in Filter_fields, but only for the equivalent, inflexible, flexible way is using Filterset, the following example: class productfilter (django_filters.rest_ Framework. Filterset): min_price = django_filters. Numberfilter (name= "Price", lookup_expr= ' GTE ') max_price = django_filters. Numberfilter (name= "Price", lookup_expr= ' LTE ') class meta: model = product fields = [' category ', ' In_ Stock ', ' min_price ', ' Max_price '] class productlist (generics. Listapiview): &Nbsp; queryset = product.objects.all () serializer_class = ProductSerializer filter_backends = (django_filters.rest_framework. Djangofilterbackend,) filter_class = productfilter "" " queryset = snippet.objects.all () title = self.request.query_params.get (' title ', none) Language = self.request.query_params.get (' language ', none) style = self.request.query_params.get (' style ', none) aq = q () if title is not none: aq.add (Q ( Title__startswith=title), q.and) if&nbsP;language is not none: aq.add (Q (language= Language, q.and) if style is not None: aq.add (Q (Style=style), q.and) queryset = Queryset.filter (AQ). order_by ("-id") return queryset
As for paging, you only need to specify the paging class in the view, or you can specify the page in settings.py
Pagination_class = standardresultssetpagination# or setting.pyrest_framework = {' Default_pagination_class ': ' Rest_ Framework.pagination.PageNumberPagination ', ' page_size ': 100}
It involves the JSON of multiple tables, re-writing serializer, attaching the required fields, and using the new serializer in view.
The
Output is the JSON that contains the user information.
get /snippet_join_users/http 200 okallow: get, head, optionscontent-type: application/jsonvary: accept{ "Count": 8, "Next": "http://127.0.0.1:8000/snippet_join_users/?page=2", "Previous": null, "Results": [ { "id": 8, "title": "Test8", "code": "test", " Linenos ": false, " language ": "ABAP", "style": "Algol", "owner": "admin", "Email": "[email protected]", "user_id": 1, "Last_login": "2016-12-11t03:50:40.909685z" }, { "id": 7, "title": " Test7 ", " code ": " test ", "Linenos": false, "Language": "ABAP", "Style": "Algol", "owner" : "admin", "email": "[email protected] ", " user_id ": 1, "Last_login": "2016-12-11t03 : 50:40.909685z " }, { "id": 6, "title": "Test6", "code": "test", "Linenos": false, "Language": "ABAP ", " style ": " Algol ", "owner": "admin", "Email": "[email protected]", "user_id": 1, "Last_login": "2016-12-11t03:50:40.909685z" }, { "id": 5, "title": "Test5", "code": "test", " Linenos ":  FALSE,   &NBsp; "Language": "ABAP", "Style": "Algol", "owner": "admin", "Email": "[email protected]", "user_id": 1, "Last_login": "2016-12-11t03:50:40.909685z" }, { "id": 4, "title": " Test4 ", " code ": " test ", "Linenos": false, "Language": "ABAP", " Style ": " Algol ", " owner ": " Admin ", " email ": " [email Protected] ", " user_id ": 1, "Last_login": "2016-12-11t03 : 50:40.909685z "        }    ]}
This article is from the "Days together with it" blog, so be sure to keep this source http://raugher.blog.51cto.com/3472678/1883488
Django-rest-framework Multi-Criteria Query/pagination/multi-table JSON