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 library that provides a set of generic views to perform CRUD operations (Create, Retrieve, Update, Delete) on Django models using Django Ninja.

Features:

  • DRY: No need to write the same code over and over again.
  • Customizable: You can customize the views to fit your needs, or even write your own views.
  • Testable: You can test your views easily, the same way you defined them.

Installation

pip install django-ninja-crud

Usage

Let's say you have a model called Department:

# models.py
from django.db import models

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

And you have a schema for serializing and deserializing the model:

# schemas.py
from ninja import Schema

class DepartmentIn(Schema):
    title: str

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

Here is a brief example of how to use django-ninja-crud:

# views.py
from examples.models import Department
from examples.schemas import DepartmentIn, DepartmentOut
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()

Testing

You can then write the associated tests like so:

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

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_instance(self):
        return self.department_1

    department_payloads = Payloads(
        ok={"title": "department-updated"},
        bad_request={"bad-key": "department-updated"},
        conflict={"title": "department-2"},
    )

    test_list = ListModelViewTest(instance_getter=get_instance)
    test_create = CreateModelViewTest(payloads=department_payloads, instance_getter=get_instance)
    test_retrieve = RetrieveModelViewTest(instance_getter=get_instance)
    test_update = UpdateModelViewTest(payloads=department_payloads, instance_getter=get_instance)
    test_delete = DeleteModelViewTest(instance_getter=get_instance)

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.1.1.tar.gz (10.1 kB view hashes)

Uploaded Source

Built Distribution

django_ninja_crud-0.1.1-py3-none-any.whl (17.2 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