DeleteView

Exploring the functionality of the DeleteView in Django Ninja CRUD

DeleteView

class DeleteView(APIView)

Declarative class-based view for deleting a model instance in Django Ninja.

This class provides a standard implementation for a delete view, which retrieves
a single model instance based on the path parameters and deletes it. It is
intended to be used in viewsets or as standalone views to simplify the creation
of delete endpoints.

Arguments:

  • name str | None, optional - View function name. Defaults to None. If None,
    uses class attribute name in viewsets or "handler" for standalone views.
  • methods List[str], optional - HTTP methods. Defaults to ["DELETE"].
  • path str, optional - URL path. Defaults to "/{id}".
  • response_status int, optional - HTTP response status code. Defaults to 204.
  • response_body Type | None, optional - Response body type. Defaults to None.
  • model Type[django.db.models.Model] | None, optional - Associated Django model.
    Inherits from viewset if not provided. Defaults to None.
  • path_parameters Type[BaseModel] | None, optional - Path parameters type.
    Defaults to None. If not provided, resolved from the path and model.
  • get_model Callable | None, optional - Retrieves model instance. Default uses
    path parameters (e.g., self.model.objects.get(id=path_parameters.id)
    for /{id} path). Useful for customizing model retrieval logic.
    Should have the signature:
    • (request: HttpRequest, path_parameters: Optional[BaseModel]) -> Model
  • pre_delete Callable | None, optional - A callable to perform pre-delete
    operations on the model instance. By default, it does nothing. Useful for
    additional operations before deleting the instance.
    Should have the signature:
    • (request: HttpRequest, instance: Model) -> None
  • post_delete Callable | None, optional - A callable to perform post-delete
    operations on the model instance. By default, it does nothing. Useful for
    additional operations after deleting the instance.
    Should have the signature:
    • (request: HttpRequest, instance: Model) -> None
  • decorators List[Callable] | None, optional - View function decorators
    (applied in reverse order). Defaults to None.
  • operation_kwargs Dict[str, Any] | None, optional - Additional operation
    keyword arguments. Defaults to None.

Example:

from ninja import NinjaAPI
from ninja_crud import views, viewsets

from examples.models import Department

api = NinjaAPI()

# Usage as a class attribute in a viewset:
class DepartmentViewSet(viewsets.APIViewSet):
    api = api
    model = Department

    delete_department = views.DeleteView()

# Usage as a standalone view:
views.DeleteView(
    name="delete_department",
    model=Department
).add_view_to(api)

set_api_viewset_class

def set_api_viewset_class(api_viewset_class: Type["APIViewSet"]) -> None

Bind the view to a viewset class.

This method sets the model and path parameters type based on the viewset class.

Notes:

This method is called internally and automatically by the viewset when
defining views as class attributes. It should not be called manually.