Skip to main content

Django Ninja CRUD is a library that allows you to create CRUD APIs in a few lines of code.

Project description

Django Ninja CRUD

example workflow Coverage PyPI version Downloads License: MIT

Django Ninja CRUD

Django Ninja CRUD is a powerful tool designed to streamline the development of CRUD (Create, Read, Update, Delete) operations in Django applications using the Django Ninja framework. It is intended to help developers save significant time by eliminating the need to write repetitive code for each operation and even more on the associated tests.

Developing these features from scratch every time can be time-consuming and error-prone. That's where Django Ninja CRUD comes in—it provides a set of predefined, customizable "blueprints" for these operations, allowing developers to set them up quickly and reliably. Instead of getting bogged down with the details of how these operations are performed, developers can focus on the unique, creative aspects of their applications.

🏄‍♂️ Key Features

Django Ninja CRUD is a flexible, transparent, and configurable solution to a common challenge in web development.

  • Flexibility: Designed to adapt to your needs. Whether you're creating, retrieving, updating, or deleting data, you can shape your operations as you see fit. It allows developers to either use the standard operations or to define their own, tailored to their specific requirements.
  • Transparency: What you see is what you get. The operations you define are the operations you perform, making it easy to understand and follow the data flow. It encourages practices that make your code easy to read and maintain. Also, it allows for easy testing of the defined operations, ensuring that everything works as expected.
  • Configurability: Provides a high degree of customization. You're not locked into a rigid structure; instead, you can adjust and fine-tune your operations to suit your project's unique needs. You can use the built-in tools to quickly set up standard operations, and if needed, you can customize them or even build your own from scratch.

By using this package, developers can write efficient and clear code, saving time and reducing potential errors. It's a tool that accommodates the developer's workflow, rather than forcing the developer to adapt to it.

📝 Requirements

Python versions Django versions Django Ninja versions

📈 Installation

pip install django-ninja-crud

For more information, see the installation guide.

👨‍🎨 Example

Usage

Let's imagine you're building a system for a university and you have a model called Department. Each department in your university has a unique title.

# models.py
from django.db import models

class Department(models.Model):
    title = models.CharField(max_length=255, unique=True)

To interact with this data, we need a way to convert it between Python objects and a format that's easy to read and write (like JSON). In Django Ninja, we do this with Schema:

# schemas.py
from ninja import Schema

class DepartmentIn(Schema):
    title: str

class DepartmentOut(Schema):
    id: int
    title: str

The DepartmentIn schema defines what data we need when creating or updating a department. The DepartmentOut schema defines what data we'll provide when retrieving a department.

Now, here comes the power of Django Ninja CRUD. With it, you can set up the CRUD operations for the Department model with just a few lines of code:

# views.py
from example.models import Department
from example.schemas import DepartmentIn, DepartmentOut
from ninja import Router
from ninja_crud.views import ModelViewSet, ListModelView, CreateModelView, \
    RetrieveModelView, UpdateModelView, DeleteModelView


class DepartmentViewSet(ModelViewSet):
    model = Department
    input_schema = DepartmentIn
    output_schema = DepartmentOut

    list = ListModelView(output_schema=output_schema)
    create = CreateModelView(input_schema=input_schema, output_schema=output_schema)
    retrieve = RetrieveModelView(output_schema=output_schema)
    update = UpdateModelView(input_schema=input_schema, output_schema=output_schema)
    delete = DeleteModelView()


router = Router()
DepartmentViewSet.register_routes(router)

Testing

A key advantage of this package is that it makes your views easy to test. Once you've set up your CRUD operations, you can write tests to ensure they're working as expected. Here's an example of how you might test the Department operations:

# tests.py
from django.test import TestCase
from example.models import Department
from example.views.view_department import DepartmentViewSet
from ninja_crud.tests import CreateModelViewTest, DeleteModelViewTest, \
    ListModelViewTest, ModelViewSetTest, RetrieveModelViewTest, UpdateModelViewTest, \
    BodyParams, PathParams


class DepartmentViewSetTest(ModelViewSetTest, TestCase):
    model_view_set = DepartmentViewSet

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

    def get_path_params(self):
        return PathParams(ok={"id": self.department_1.id}, not_found={"id": 9999})

    body_params = BodyParams(
        ok={"title": "new_title"},
        bad_request={"bad-title": 1},
        conflict={"title": "department-2"},
    )

    test_list = ListModelViewTest()
    test_create = CreateModelViewTest(body_params=body_params)
    test_retrieve = RetrieveModelViewTest(path_params=get_path_params)
    test_update = UpdateModelViewTest(path_params=get_path_params, body_params=body_params)
    test_delete = DeleteModelViewTest(path_params=get_path_params)

With this package, these tests can be written in a consistent, straightforward way, making it easier to ensure your views are working as expected.

In summary, this package simplifies the process of setting up and testing CRUD operations in Django Ninja, letting you focus on what makes your application unique. By providing a flexible, transparent, and configurable solution, this package is a powerful tool for accelerating web development.

📚 Documentation

For more information, see the documentation.

🫶 Support

"Buy Me A Coffee"

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_ninja_crud-0.2.0.tar.gz (14.0 kB view hashes)

Uploaded Source

Built Distribution

django_ninja_crud-0.2.0-py3-none-any.whl (19.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page