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 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
📈 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, 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)
With this package, these tests can be written in a consistent, straightforward way, making it easier to ensure your views are working as expected. Once you've written these tests, you can run them whenever you make changes to your views, giving you confidence that your changes haven't broken anything.
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
https://django-ninja-crud.readme.io/docs
🫶 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
Built Distribution
Hashes for django_ninja_crud-0.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4925da500649f266ad0f9b763f14b3e3cb8bd336cefc098143253a4bb462efe1 |
|
MD5 | fca3cceabc5896d5dc9771d8e30ae839 |
|
BLAKE2b-256 | 57b3e535cd64db52b234f56f519bb25c2435181f1e07c2049895f3a18e67bb94 |