6.1. Using Class Based Views (CBV)

6.1.1. 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

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/

6.1.1.1. API Filters

  • Enable filter:

    To enable a filter for the API you just need to add the filter you want in api_filters

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:

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

6.1.2. 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

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/

6.1.3. 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

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/

6.1.4. ClientAPIUpdateView

  • Simple

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

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/

6.1.5. 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

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/