Using Class Based Views (CBV) ============================= ClientAPIListView ----------------- Following the same example as Model ListView, the results returned from the model will go to the context variable ``object_list``, unless you want to customize it. Using ClientAPIListView we will use the attribute ``client_method`` instead of ``model`` to get our results .. code-block:: python from django_api_client.views import ClientAPIListView from folder_project.clients import api_client ... class TestListView(ClientAPIListView): template_name = "template_name.html" page_title = 'Tests' page_base_url = reverse_lazy('order:list') paginate_by = 50 client_method = api_client.order.orders.list .. hint:: - template_name: path with name of you template - client_method: method of the client that get a list of records of a resource list - page_title: Generates a context variable to use in your template - page_base_url: Information used in pagination and the search - paginate_by: Number of items to generate the pagination .. note:: The usage example assumes that the endpoint is ``/order/orders/`` API Filters ~~~~~~~~~~~ - Enable filter: To enable a filter for the API you just need to add the filter you want in `api_filters` .. code-block:: python from django_api_client.views import ClientAPIListView from folder_project.clients import api_client ... class TestUsingFilterListView(ClientAPIListView): template_name = "template_name.html" page_title = 'Tests' page_base_url = reverse_lazy('order:list') paginate_by = 50 client_method = api_client.order.orders.list api_filters = ['user'] .. note:: The usage example use the `user`, so if you pass the user on the querystring, this value will be passed to the API - Add a custom filter or queryset: To customize a search or call to the API you need to use `get_api_params` adding the parameters you want. Example: .. code-block:: python from django_api_client.views import ClientAPIListView from folder_project.clients import api_client ... class TestUsingCustomFilterListView(ClientAPIListView): template_name = "template_name.html" page_title = 'Tests' page_base_url = reverse_lazy('order:list') paginate_by = 50 client_method = api_client.order.orders.list api_filters = ['user'] def get_extra_params(self, request): extra_params = super().get_extra_params(request) extra_params['status'] = 'finished' return extra_params .. note:: The usage example take all params you already have and add the custom `status` to send to the API ClientAPIDetailView ------------------- Following the same example as Model DetailView, the results returned from the model will go to the context variable ``object``, unless you want to customize it. Using ClientAPIDetailView we will use the attribute ``client_method`` instead of ``model`` to get our result .. code-block:: python from django_api_client.views import ClientAPIDetailView from folder_project.clients import api_client ... class TestDetailView(ClientAPIDetailView): template_name = "template_name.html" page_title = _('View Test') client_method = api_client.order.orders.get .. hint:: - template_name: path with name of you template - client_method: method of the client that to get a record in the resource - page_title: Generates a context variable to use in your template .. note:: The usage example assumes that the endpoint is ``/order/orders/`` ClientAPICreateView ------------------- Following the same example as Model CreateView, on submit will validate and save, using ClientAPIListView we will use the attribute `` client_method`` instead of ``model`` to get our result .. code-block:: python from django_api_client.views import ClientAPICreateView from folder_project.clients import api_client ... class TestCreateView(ClientAPICreateView): form_class = TestForm template_name = "template_name.html" page_title = _('View Test') success_url = reverse_lazy('test:list') client_method = api_client.order.orders.create .. hint:: - template_name: path with name of you template - client_method: method of the client that create a record in the resource - page_title: Generates a context variable to use in your template .. note:: The usage example assumes that the endpoint is ``/order/orders/`` ClientAPIUpdateView ------------------- - Simple .. code-block:: python from django_api_client.views import ClientAPIUpdateView from folder_project.clients import api_client ... class TestUpdateView(ClientAPIUpdateView): form_class = TestForm template_name = "template_name.html" success_url = reverse_lazy('test:list') page_title = _('Edit Test') client_method = api_client.order.orders.update client_initial_method = api_client.order.orders.get partial = False - Advanced with a custom initial .. code-block:: python from django_api_client.views import ClientAPIUpdateView from folder_project.clients import api_client ... class TestUpdateView(ClientAPIUpdateView): form_class = TestForm template_name = "template_name.html" success_url = reverse_lazy('test:list') page_title = _('Edit Test') client_method = api_client.order.orders.update client_initial_method = api_client.order.orders.get partial = False def get_initial(self): response = self.client_initial_method(**self.kwargs) data = response.as_dict() instance = response.as_obj() data['start_date'] = datetime.fromisoformat(instance.start_date).strftime('%d/%m/%Y %H:%M') data['end_date'] = datetime.fromisoformat(instance.end_date).strftime('%d/%m/%Y %H:%M') return data .. hint:: - template_name: path with name of you template - client_method: method of the client that to update - client_initial_method: method of the client that brings the result - page_title: Generates a context variable to use in your template - partial: means if you are going to update only part of your asset or you are going to update everything .. note:: The usage example assumes that the endpoint is ``/order/orders/`` ClientAPIDeleteView ------------------- Following the same example as Model DetailView, the results returned from the model will go to the context variable ``object``, unless you want to customize it. Using ClientAPIDetailView we will use the attribute ``client_method`` instead of ``model`` to get our result .. code-block:: python from django_api_client.views import ClientAPIDeleteView from folder_project.clients import api_client ... class TestDetailView(ClientAPIDeleteView): client_method = api_client.order.orders.delete success_url = reverse_lazy('test:list') .. hint:: - client_method: method of the client that to remove the resource .. note:: The usage example assumes that the endpoint is ``/order/orders/``