Skip to content

Commit 95e2387

Browse files
committed
document update
1 parent f4b8afd commit 95e2387

File tree

1 file changed

+21
-57
lines changed

1 file changed

+21
-57
lines changed

docs/docs/api_controller/index.md

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ So I designed APIController to extend Django-Ninja to class-based and have somet
1414

1515
So if you enjoy class-based controls for building API, welcome aboard.
1616

17+
18+
## ControllerBase
19+
```python
20+
class ControllerBase(ABC):
21+
...
22+
```
23+
APIController decorates any class with `ControllerBase` if its not inheriting from it.
24+
25+
!!!info
26+
Inheriting from ControllerBase class gives you more IDE intellisense support.
27+
1728
## Quick Example
1829
Let's create an APIController to manage Django user model
1930

@@ -23,7 +34,7 @@ from typing import List
2334
from ninja import ModelSchema
2435
from ninja_extra import (
2536
http_get, http_post, http_generic, http_delete,
26-
api_controller, exceptions, status
37+
api_controller, status, ControllerBase, pagination
2738
)
2839
from ninja_extra.controllers.response import Detail
2940
from django.contrib.auth import get_user_model
@@ -36,7 +47,7 @@ class UserSchema(ModelSchema):
3647

3748

3849
@api_controller('/users')
39-
class UsersController:
50+
class UsersController(ControllerBase):
4051
user_model = get_user_model()
4152

4253
@http_post('')
@@ -47,69 +58,22 @@ class UsersController:
4758
@http_generic('/{int:user_id}', methods=['put', 'patch'], response=UserSchema)
4859
def update_user(self, user_id: int):
4960
""" Django Ninja will serialize Django ORM model to schema provided as `response`"""
50-
user = self.user_model.objects.filter(id=user_id).first()
51-
if user:
52-
return user
53-
raise exceptions.NotFound(f'User with id: `{user_id}` not found')
61+
user = self.get_object_or_exception(self.user_model, id=user_id)
62+
return user
5463

5564
@http_delete('/{int:user_id}', response=Detail(status_code=status.HTTP_204_NO_CONTENT))
5665
def delete_user(self, user_id: int):
57-
user = self.user_model.objects.filter(id=user_id).first()
58-
if user:
59-
user.delete()
60-
return self.create_response('', status_code=status.HTTP_204_NO_CONTENT)
61-
raise exceptions.NotFound(f'User with id: `{user_id}` not found')
66+
user = self.get_object_or_exception(self.user_model, id=user_id)
67+
user.delete()
68+
return self.create_response('', status_code=status.HTTP_204_NO_CONTENT)
6269

6370
@http_get('', response=List[UserSchema])
71+
@pagination.paginate
6472
def list_user(self):
6573
return self.user_model.objects.all()
6674

6775
@http_get('/{user_id}', response=UserSchema)
6876
def get_user_by_id(self, user_id: int):
69-
user = self.user_model.objects.filter(id=user_id).first()
70-
if user:
71-
return user
72-
raise exceptions.NotFound(f'User with id: `{user_id}` not found')
77+
user = self.get_object_or_exception(self.user_model, id=user_id)
78+
return user
7379
```
74-
75-
```python
76-
class ControllerBase:
77-
...
78-
```
79-
80-
## **Model Properties**
81-
- ### **`permission_classes`**
82-
List of default permission classes defined in a controller `router`
83-
84-
- ### **`auth`**
85-
List of default Authentication instances. As described in Django-Ninja [Authentication](https://django-ninja.rest-framework.com/tutorial/authentication/). default: `[]`
86-
87-
- ### **`api`**
88-
Instance of NinjaExtraAPI at runtime. default:`None`
89-
90-
- ### **`auto_import`**
91-
states whether APIController should added to auto_controller import list. default: `True`
92-
93-
- ### **`get_router(cls)`**
94-
return controller to router instance if present and raises Exception is absent.
95-
96-
- ### **`get_path_operations(cls)`**
97-
container `dict` of route definition which are pass to Django-Ninja at runtime
98-
99-
- ### **`add_operation_from_route_function(cls, route_function: RouteFunction)`**
100-
A method overload for `add_api_operation`
101-
102-
- ### **`add_api_operation(cls, ...)`**
103-
Adds APIController route definitions to path operation
104-
105-
- ### **`get_route_functions(cls)`**
106-
Gets all registered route in an APIController
107-
108-
- ### **`get_permissions(self)`**
109-
Returns list of `permission_classes` instances
110-
111-
- ### **`check_permissions(self)`**
112-
Check permission when route function is invoked
113-
114-
- ### **`check_object_permissions(self, obj: Any)`**
115-
Checks object permissions. This is not automated. However, when called, it triggers all `permission_classes` `has_object_permission` function, just like in DRF

0 commit comments

Comments
 (0)