Welcome to Django Ninja CRUD

Django Ninja CRUD is a powerful, declarative, and yet opinionated framework that simplifies the development of CRUD (Create, Read, Update, Delete) views and tests with Django Ninja.
It promotes best practices for efficient, robust endpoint creation, allowing you to focus on what matters most: solving real problems.
Initially inspired by DRF's ModelViewSet, Django Ninja CRUD evolved to address its limitations, adopting a composition-over-inheritance approach to achieve true modularity – a foundational step towards a broader declarative interface for endpoint creation.

Key Features - The Cornerstones of Django Ninja CRUD

  • Purely Declarative: Embrace an approach where defining views and tests is a matter of declaring what you want, not how to achieve it.
  • Unmatched Modularity: Tailor your viewsets with the desired CRUD views and customize each view's behavior with ease. Extend the flexibility by creating your own subclasses of the provided views and tests.
  • Powerful Testing Framework: Leverage a matrix-based testing framework for defining diverse test scenarios declaratively.
  • Focus on What Matters: Spend more time solving real-world problems and less on CRUD boilerplate.
from ninja import Router
from ninja_crud import views, viewsets

from examples.models import Department
from examples.schemas import DepartmentIn, DepartmentOut

router = Router()


class DepartmentViewSet(viewsets.ModelViewSet):
    model = Department
    default_input_schema = DepartmentIn
    default_output_schema = DepartmentOut

    list_view = views.ListModelView()
    create_view = views.CreateModelView()
    retrieve_view = views.RetrieveModelView()
    update_view = views.UpdateModelView()
    delete_view = views.DeleteModelView()


DepartmentViewSet.register_routes(router)

Declarative CRUD ViewSets Redefined

Step into the future of Django CRUD operations with our ModelViewSet. Our framework reimagines endpoint creation through a declarative lens, simplifying complex processes into concise, readable code. Grounded in best practices, the ModelViewSet encapsulates the essence of efficient and robust design. By specifying models and schemas declaratively, you can rapidly generate fully-fledged CRUD operations, giving you the freedom to tackle the real challenges that matter. This is where the journey of redefining web application development begins.

from ninja_crud import testing

from examples.models import Department
from examples.views import DepartmentViewSet


class TestDepartmentViewSet(testing.viewsets.ModelViewSetTestCase):
    model_viewset_class = DepartmentViewSet
    base_path = "api/departments"

    @classmethod
    def setUpTestData(cls):
        cls.department_1 = Department.objects.create(title="department-1")
        cls.department_2 = Department.objects.create(title="department-2")

    @property
    def path_parameters(self):
        return testing.components.PathParameters(
            ok={"id": self.department_1.id},
            not_found={"id": 999}
        )

    @property
    def payloads(self):
        return testing.components.Payloads(
            ok={"title": "department-3"},
            bad_request={"title": ""},
            conflict={"title": self.department_2.title}
        )

    test_list_view = testing.views.ListModelViewTest()
    test_create_view = testing.views.CreateModelViewTest(payloads)
    test_retrieve_view = testing.views.RetrieveModelViewTest(path_parameters)
    test_update_view = testing.views.UpdateModelViewTest(path_parameters, payloads)
    test_delete_view = testing.views.DeleteModelViewTest(path_parameters)

Precision Testing with ModelViewSetTestCase

Quality is not just a feature; it's a cornerstone of development. With our ModelViewSetTestCase, testing is no longer a chore but a seamless part of the development process. Our framework empowers you to declare test cases with the same precision and elegance as your views. By defining path parameters and payloads, you can create a comprehensive matrix of tests that ensure your CRUD endpoints are not only functional but bulletproof. Dive into a testing experience that complements the declarative nature of Django Ninja CRUD, and deliver quality that speaks volumes about your commitment to excellence.