Django Ninja CRUD is a library that allows you to create CRUD APIs in a few lines of code.
Project description
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 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
You can then write the associated tests like so:
# 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, 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
Project details
Release history Release notifications | RSS feed
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.2.tar.gz
(10.5 kB
view hashes)
Built Distribution
Close
Hashes for django_ninja_crud-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75cc1068fa82b5bffbe5f2e26b68222ab0bfeee016fb5051fb9b9d4437bbbf11 |
|
MD5 | 8e6c7b1936fe0a82bb8e88a15e55f113 |
|
BLAKE2b-256 | 38f43b58e3cd8e735fa8a4e1e4249983f8ad76b8d1731df099de17bafaea148e |