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 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
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.1.tar.gz
(10.1 kB
view hashes)
Built Distribution
Close
Hashes for django_ninja_crud-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0f1bd7f2341000cc0869b87e6155d63c91594b4e525103ec8aca9187e9517ed |
|
MD5 | 0b108da7eeeaf02ce2af56c3285b9610 |
|
BLAKE2b-256 | 1d061bba748fe493f7a485461b89256c857085bd0e8d678eb22d86927aa407ba |