Understanding and applying the UpdateView in Django Ninja CRUD
UpdateView
class UpdateView(APIView)
Declarative class-based view for updating a model instance in Django Ninja.
This class provides a standard implementation for an update view, which retrieves
a single model instance based on the path parameters, updates the instance based on
the request body, and saves the changes to the database. It is intended to be used
in viewsets or as standalone views to simplify the creation of update endpoints.
Arguments:
namestr | None, optional - View function name. Defaults toNone. If None,
uses class attribute name in viewsets or "handler" for standalone views.methodsList[str], optional - HTTP methods. Defaults to["PUT"].pathstr, optional - URL path. Defaults to"/{id}".response_statusint, optional - HTTP response status code. Defaults to200.response_bodyType | None, optional - Response body type. Defaults toNone.
If None, uses the default response body of the viewset.modelType[django.db.models.Model] | None, optional - Associated Django model.
Inherits from viewset if not provided. Defaults toNone.path_parametersType[BaseModel] | None, optional - Path parameters type.
Defaults toNone. If not provided, resolved from the path and model.request_bodyType[BaseModel] | None, optional - The request body type.
Defaults toNone. If None, uses the default request body of the viewset.get_modelCallable | 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_saveCallable | None, optional - Pre-save operations on the model instance.
Default callsfull_cleanon the instance. Should have the signature:(request: HttpRequest, instance: Model) -> None
post_saveCallable | None, optional - Post-save operations on the model instance.
Default does nothing. Should have the signature:(request: HttpRequest, instance: Model) -> None
decoratorsList[Callable] | None, optional - View function decorators
(applied in reverse order). Defaults toNone.operation_kwargsDict[str, Any] | None, optional - Additional operation
keyword arguments. Defaults toNone.
Example:
from ninja import NinjaAPI
from ninja_crud import views, viewsets
from examples.models import Department
from examples.schemas import DepartmentIn, DepartmentOut
api = NinjaAPI()
# Usage as a class attribute in a viewset:
class DepartmentViewSet(viewsets.APIViewSet):
api = api
model = Department
default_request_body = DepartmentIn
default_response_body = DepartmentOut
# Usage with default request and response bodies:
update_department = views.UpdateView()
# Usage with explicit request and response bodies:
update_department = views.UpdateView(
request_body=DepartmentIn,
response_body=DepartmentOut,
)
# Usage as a standalone view:
views.UpdateView(
name="update_department",
model=Department,
request_body=DepartmentIn,
response_body=DepartmentOut,
).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,
and assigns the request body and response body from the viewset class's
default_request_body and default_response_body, respectively, if they are
not already set.
Notes:
This method is called internally and automatically by the viewset when
defining views as class attributes. It should not be called manually.
