Skip to content

Commit 7848048

Browse files
authored
Merge pull request #228 from eadwinCode/readme_test
fix: Readme And Query Testing
2 parents 2da191d + a81b5e7 commit 7848048

File tree

6 files changed

+968
-123
lines changed

6 files changed

+968
-123
lines changed

README.md

Lines changed: 146 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,106 +8,191 @@
88

99
# Django Ninja Extra
1010

11-
**Django Ninja Extra** package offers a **class-based** approach plus extra functionalities that will speed up your RESTful API development with [**Django Ninja**](https://django-ninja.rest-framework.com)
11+
## Overview
1212

13-
**Key features:**
13+
Django Ninja Extra is a powerful extension for [Django Ninja](https://django-ninja.rest-framework.com) that enhances your Django REST API development experience. It introduces class-based views and advanced features while maintaining the high performance and simplicity of Django Ninja. Whether you're building a small API or a large-scale application, Django Ninja Extra provides the tools you need for clean, maintainable, and efficient API development.
1414

15-
All **Django-Ninja** features :
16-
- **Easy**: Designed to be easy to use and intuitive.
17-
- **FAST execution**: Very high performance thanks to **<a href="https://pydantic-docs.helpmanual.io" target="_blank">Pydantic</a>** and **<a href="/async-support/">async support</a>**.
18-
- **Fast to code**: Type hints and automatic docs lets you focus only on business logic.
19-
- **Standards-based**: Based on the open standards for APIs: **OpenAPI** (previously known as Swagger) and **JSON Schema**.
20-
- **Django friendly**: (obviously) has good integration with the Django core and ORM.
15+
## Features
2116

22-
Plus **Extra**:
23-
- **Class Based**: Design your APIs in a class based fashion.
24-
- **Permissions**: Protect endpoint(s) at ease with defined permissions and authorizations at route level or controller level.
25-
- **Dependency Injection**: Controller classes supports dependency injection with python [**Injector** ](https://injector.readthedocs.io/en/latest/) or [**django_injector**](https://github.com/blubber/django_injector). Giving you the ability to inject API dependable services to APIController class and utilizing them where needed
17+
### Core Features (Inherited from Django Ninja)
18+
-**High Performance**: Built on Pydantic for lightning-fast validation
19+
- 🔄 **Async Support**: First-class support for async/await operations
20+
- 📝 **Type Safety**: Comprehensive type hints for better development experience
21+
- 🎯 **Django Integration**: Seamless integration with Django's ecosystem
22+
- 📚 **OpenAPI Support**: Automatic API documentation with Swagger/ReDoc
2623

27-
---
24+
### Extra Features
25+
- 🏗️ **Class-Based Controllers**:
26+
- Organize related endpoints in controller classes
27+
- Inherit common functionality
28+
- Share dependencies across endpoints
29+
30+
- 🔒 **Advanced Permission System (Similar to Django Rest Framework)**:
31+
- Controller-level permissions
32+
- Route-level permission overrides
33+
- Custom permission classes
34+
35+
- 💉 **Dependency Injection**:
36+
- Built-in support for [Injector](https://injector.readthedocs.io/en/latest/)
37+
- Compatible with [django_injector](https://github.com/blubber/django_injector)
38+
- Automatic dependency resolution
39+
40+
- 🔧 **Service Layer**:
41+
- Injectable services for business logic
42+
- Better separation of concerns
43+
- Reusable components
44+
45+
## Requirements
2846

29-
### Requirements
3047
- Python >= 3.6
31-
- django >= 2.1
32-
- pydantic >= 1.6
48+
- Django >= 2.1
49+
- Pydantic >= 1.6
3350
- Django-Ninja >= 0.16.1
3451

35-
Full documentation, [visit](https://eadwincode.github.io/django-ninja-extra/).
36-
3752
## Installation
3853

39-
```
54+
1. Install the package:
55+
```bash
4056
pip install django-ninja-extra
4157
```
42-
After installation, add `ninja_extra` to your `INSTALLED_APPS`
4358

44-
```Python
59+
2. Add to INSTALLED_APPS:
60+
```python
4561
INSTALLED_APPS = [
4662
...,
4763
'ninja_extra',
4864
]
4965
```
5066

51-
## Usage
67+
## Quick Start Guide
68+
69+
### 1. Basic API Setup
5270

53-
In your django project next to urls.py create new `api.py` file:
71+
Create `api.py` in your Django project:
5472

55-
```Python
73+
```python
5674
from ninja_extra import NinjaExtraAPI, api_controller, http_get
5775

5876
api = NinjaExtraAPI()
5977

60-
# function based definition
61-
@api.get("/add", tags=['Math'])
62-
def add(request, a: int, b: int):
63-
return {"result": a + b}
64-
65-
#class based definition
66-
@api_controller
67-
class MathAPI:
68-
69-
@http_get('/subtract',)
70-
def subtract(self, a: int, b: int):
71-
"""Subtracts a from b"""
72-
return {"result": a - b}
73-
74-
@http_get('/divide',)
75-
def divide(self, a: int, b: int):
76-
"""Divides a by b"""
77-
return {"result": a / b}
78-
79-
@http_get('/multiple',)
80-
def multiple(self, a: int, b: int):
81-
"""Multiples a with b"""
78+
# Function-based endpoint example
79+
@api.get("/hello", tags=['Basic'])
80+
def hello(request, name: str = "World"):
81+
return {"message": f"Hello, {name}!"}
82+
83+
# Class-based controller example
84+
@api_controller('/math', tags=['Math'])
85+
class MathController:
86+
@http_get('/add')
87+
def add(self, a: int, b: int):
88+
"""Add two numbers"""
89+
return {"result": a + b}
90+
91+
@http_get('/multiply')
92+
def multiply(self, a: int, b: int):
93+
"""Multiply two numbers"""
8294
return {"result": a * b}
83-
84-
api.register_controllers(
85-
MathAPI
86-
)
95+
96+
# Register your controllers
97+
api.register_controllers(MathController)
8798
```
8899

89-
Now go to `urls.py` and add the following:
100+
### 2. URL Configuration
90101

91-
```Python
92-
...
102+
In `urls.py`:
103+
```python
93104
from django.urls import path
94105
from .api import api
95106

96107
urlpatterns = [
97-
path("admin/", admin.site.urls),
98-
path("api/", api.urls), # <---------- !
108+
path("api/", api.urls), # This will mount your API at /api/
99109
]
110+
```
111+
112+
## Advanced Features
113+
114+
### Authentication and Permissions
115+
116+
```python
117+
from ninja_extra import api_controller, http_get
118+
from ninja_extra.permissions import IsAuthenticated, PermissionBase
119+
120+
# Custom permission
121+
class IsAdmin(PermissionBase):
122+
def has_permission(self, context):
123+
return context.request.user.is_staff
100124

125+
@api_controller('/admin', tags=['Admin'], permissions=[IsAuthenticated, IsAdmin])
126+
class AdminController:
127+
@http_get('/stats')
128+
def get_stats(self):
129+
return {"status": "admin only data"}
130+
131+
@http_get('/public', permissions=[]) # Override to make public
132+
def public_stats(self):
133+
return {"status": "public data"}
101134
```
102135

103-
### Interactive API docs
136+
### Dependency Injection with Services
137+
138+
```python
139+
from injector import inject
140+
from ninja_extra import api_controller, http_get
141+
104142

105-
Now go to <a href="http://127.0.0.1:8000/api/docs" target="_blank">http://127.0.0.1:8000/api/docs</a>
143+
# Service class
144+
class UserService:
145+
def get_user_details(self, user_id: int):
146+
return {"user_id": user_id, "status": "active"}
106147

107-
You will see the automatic interactive API documentation (provided by <a href="https://github.com/swagger-api/swagger-ui" target="_blank">Swagger UI</a>):
148+
149+
# Controller with dependency injection
150+
@api_controller('/users', tags=['Users'])
151+
class UserController:
152+
def __init__(self, user_service: UserService):
153+
self.user_service = user_service
154+
155+
@http_get('/{user_id}')
156+
def get_user(self, user_id: int):
157+
return self.user_service.get_user_details(user_id)
158+
```
159+
160+
## API Documentation
161+
162+
Access your API's interactive documentation at `/api/docs`:
108163

109164
![Swagger UI](docs/images/ui_swagger_preview_readme.gif)
110165

111-
## Tutorials
112-
- [django-ninja - Permissions, Controllers & Throttling with django-ninja-extra!](https://www.youtube.com/watch?v=yQqig-c2dd4) - Learn how to use permissions, controllers and throttling with django-ninja-extra
113-
- [BookStore API](https://github.com/eadwinCode/bookstoreapi) - A sample project that demonstrates how to use django-ninja-extra with ninja schema and ninja-jwt
166+
## Learning Resources
167+
168+
### Tutorials
169+
- 📺 [Video: Permissions & Controllers](https://www.youtube.com/watch?v=yQqig-c2dd4)
170+
- 💻 [Example: BookStore API](https://github.com/eadwinCode/bookstoreapi)
171+
- 📚 [Official Documentation](https://eadwincode.github.io/django-ninja-extra/)
172+
173+
### Community and Support
174+
- 🌟 [GitHub Repository](https://github.com/eadwinCode/django-ninja-extra)
175+
- 🐛 [Issue Tracker](https://github.com/eadwinCode/django-ninja-extra/issues)
176+
- 💬 [Discussions](https://github.com/eadwinCode/django-ninja-extra/discussions)
177+
178+
## Contributing
179+
180+
We welcome contributions! Here's how you can help:
181+
182+
1. Fork the repository
183+
2. Create a feature branch
184+
3. Write your changes
185+
4. Submit a pull request
186+
187+
Please ensure your code follows our coding standards and includes appropriate tests.
188+
189+
## License
190+
191+
This project is licensed under the MIT License - see the LICENSE file for details.
192+
193+
## Support the Project
194+
195+
- ⭐ Star the repository
196+
- 🐛 Report issues
197+
- 📖 Contribute to documentation
198+
- 🤝 Submit pull requests

0 commit comments

Comments
 (0)