Overview and usage patterns of the AbstractModelView in Django Ninja CRUD
AbstractModelView
class AbstractModelView(ABC)
An abstract base class for all model views.
Subclasses must implement the register_route
, get_response
, get_operation_id
, and get_summary
methods.
__init__
def __init__(method: HTTPMethod,
path: str,
detail: bool,
decorators: Optional[List[Callable]] = None,
router_kwargs: Optional[dict] = None) -> None
Initializes the AbstractModelView with the given decorators and optional router keyword arguments.
Arguments:
method
HTTPMethod - The HTTP method for the view.path
str - The path to use for the view.detail
bool - Whether the view is for a detail or collection route.decorators
Optional[List[Callable]], optional - A list of decorators to apply to the view. Defaults to [].router_kwargs
Optional[dict], optional - Additional arguments to pass to the router. Defaults to {}.
Overrides are allowed for most arguments except 'path', 'methods', and 'response'. If any of these
arguments are provided, a warning will be logged and the override will be ignored.
register_route
@abstractmethod
def register_route(router: Router, model_class: Type[Model]) -> None
Registers the route with the given router.
Arguments:
router
Router - The router to register the route with.model_class
Type[Model] - The Django model class for which the route should be created.
Raises:
NotImplementedError
- This method must be implemented by a subclass.
Notes:
This method should be called by the register_routes
method of the ModelViewSet
subclass.
It should not be called directly.
configure_route
def configure_route(router: Router, model_class: Type[Model])
Configures the route for the given model class with the specified router.
This method is a key part of the route setup process. It returns a decorator which, when applied to a view
function in a subclass (like RetrieveModelView
), automatically handles the necessary configuration for
routing, including applying any specified decorators and merging router keyword arguments.
The returned decorator abstracts the intricacies of route configuration, allowing subclasses to focus on the
specific logic of the view (e.g., retrieval, creation, etc.).
Arguments:
router
Router - The Django Ninja router to register the route with.model_class
Type[Model] - The Django model class associated with this route.
Returns:
Callable
- A decorator for the route function in the subclass, encapsulating the route configuration logic.
Example:
In a subclass like RetrieveModelView
:
def register_route(self, router: Router, model_class: Type[Model]) -> None:
@self.configure_route(router=router, model_class=model_class)
def retrieve_model(request: HttpRequest, id: Any):
# ... view-specific logic ...
Notes:
This method should not be called directly by end users. It is used internally in the register_route
method of subclasses.
get_response
@abstractmethod
def get_response() -> dict
Provides a mapping of HTTP status codes to response schemas for the view.
This response schema is used in API documentation to describe the response body for this view.
The response schema is critical and cannot be overridden using router_kwargs
. Any overrides
will be ignored.
Returns:
dict
- A mapping of HTTP status codes to response schemas for the view.
Raises:
NotImplementedError
- This method must be implemented by a subclass.
get_operation_id
@abstractmethod
def get_operation_id(model_class: Type[Model]) -> str
Provides an operation ID for the view.
This operation ID is used in API documentation to uniquely identify this view.
It can be overriden using the router_kwargs
.
Arguments:
model_class
Type[Model] - The Django model class for which the route should be created.
Returns:
str
- The operation ID for the view.
Raises:
NotImplementedError
- This method must be implemented by a subclass.
get_summary
@abstractmethod
def get_summary(model_class: Type[Model]) -> str
Provides a summary description for the view.
This summary is used in API documentation to give a brief description of what this view does.
It can be overriden using the router_kwargs
.
Arguments:
model_class
Type[Model] - The Django model class for which the route should be created.
Returns:
str
- The summary for the view.
Raises:
NotImplementedError
- This method must be implemented by a subclass.